Skip to content

Commit

Permalink
Simplify the mechanisms behind bind* functions. (#111)
Browse files Browse the repository at this point in the history
* Fix return type for getElementById, closes #65

* Update PHPDoc to include inherited changes

* Rename function for removing template attributes

* Set value for certain input fields differently

* Bind list to document fragment for efficiency
for #77

* Remove unbindable check

* Allow returning any iterable, not just array - fixes #88

* Test that multiple kvps can be bound within the same parameter

* Isolate bug for #105 - use README example

* Rename BindDataGetter -> BindObject

* Tidy up bindData function

* Test incompatible bindData types

* Remove bindNestedList function

* Testing bindValue

* Test bindObjectValue

* Test bindValue within lists

* Bindable type checking

* Simplify bind functions, closes #110

* Skip empty lists
  • Loading branch information
g105b committed Nov 27, 2019
1 parent a9bd99b commit 7468beb
Show file tree
Hide file tree
Showing 15 changed files with 681 additions and 220 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Consider a page with an unordered list (`<ul>`). Within the list there should be

In this example, we will simply use an array to contain the data, but the data can come from a data source such as a database, as long as it is `iterable`.

### Source HTML (`example.html`)
### Source HTML ([`01-groceries-example.html`][example-groceries-html])

```html
<!doctype html>
Expand Down Expand Up @@ -94,3 +94,4 @@ Features at a glance
+ Use standards compliant techniques for templates and components.

[dom]: https://www.php.gt/dom
[example-groceries-html]: https://github.com/PhpGt/DomTemplate/blob/master/example/01-example-groceries.html
6 changes: 6 additions & 0 deletions example/01-example-groceries.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!doctype html>
<h1>Shopping list</h1>

<shopping-list id="groceries"></shopping-list>

<p>The use of a custom element is more useful on more complex pages, but is shown above as an example.</p>
20 changes: 20 additions & 0 deletions example/01-example-groceries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
require __DIR__ . "/../vendor/autoload.php";

use Gt\DomTemplate\HTMLDocument;

$html = file_get_contents("01-example-groceries.html");
$document = new HTMLDocument($html, "./_component");
$document->extractTemplates();

$data = [
["id" => 1, "title" => "Tomatoes"],
["id" => 2, "title" => "Noodles"],
["id" => 3, "title" => "Cheese"],
["id" => 4, "title" => "Broccoli"],
];

$outputTo = $document->getElementById("groceries");
$outputTo->bindList($data);

echo $document;
3 changes: 3 additions & 0 deletions example/_component/shopping-list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<ul>
<li data-template data-bind:text="title" data-bind:data-id="id">Item name</li>
</ul>
16 changes: 0 additions & 16 deletions src/BindDataGetter.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/BindDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface BindDataMapper {
* "age" => date("Y") - $this->dob->format("Y"),
* ]
*/
public function bindDataMap():iterable;
public function bindDataMap():array;
}
15 changes: 15 additions & 0 deletions src/BindObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Gt\DomTemplate;

/**
* This interface does not define any functions, but is used to indicate that
* the implementing class should expose "bind*" functions for the key-value data
* it represents.
*
* For example: class Person implements BindObject {
* // This function will be called for the "name" key when bound.
* public function bindName():string {
* return $this->forename . " " . $this->surname;
* }
*/
interface BindObject {}

0 comments on commit 7468beb

Please sign in to comment.