phpgt / domtemplate
Bind dynamic data to reusable HTML components.
Fund package maintenance!
PhpGt
Installs: 6 421
Dependents: 2
Suggesters: 0
Security: 0
Stars: 17
Watchers: 5
Forks: 4
Open Issues: 25
Requires
- php: >=8.1
- ext-dom: *
- phpgt/dom: ^4.0
Requires (Dev)
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^10.0
- dev-master
- v3.4.1
- v3.4.0
- v3.3.0
- v3.2.3
- v3.2.2
- v3.2.1
- v3.2.0
- v3.1.6
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.0
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.1
- v2.0.0
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.9
- v1.2.8
- v1.2.7
- v1.2.6
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.2
- v1.0.1
- v1.0.0
- v0.7.0
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.1
- v0.5.0
- v0.4.1
- v0.4.0
- v0.3.0
- v0.2.0
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- dev-478-element-binder
- dev-iterator-aggregate
- dev-dependabot/composer/phpgt/dom-4.1.4
This package is auto-updated.
Last update: 2024-11-12 15:47:29 UTC
README
Built on top of PHP.Gt/Dom, this project provides dynamic data binding to DOM Documents, document templating and reusable HTML components.
Directly manipulating the DOM in your code can lead to tightly coupling the logic and view. Binding data using custom elements and data attributes leads to highly readable, maintainable view files that are loosely coupled to the application logic.
Example usage: Hello, you!
Consider a page with a form, with an input element to enter your name. When the form is submitted, the page should greet you by your name.
Without submitting the form, the HTML will be rendered untouched, showing the default message "Hello, you!".
Source HTML (name.html
)
The HTML below shows a basic "Hello, you" message, but due to the data-bind:text
attribute, when the form is submitted, the PHP code binds the submitted name to the DOM at the correct place, greeting the user.
greeter.html
:
<!doctype html> <h1> Hello, <span data-bind:text="name-output">you</span>! </h1> <form> <input name="name" placeholder="Your name, please" required /> <button>Submit</button> </form>
PHP used to inject your name
<?php $html = file_get_contents("greeter.html"); $document = new \Gt\Dom\HTMLDocument($html); if($name = $_GET["name"]) { $binder = new \Gt\DomTemplate\DocumentBinder($document); $binder->bindKeyValue("name-output", $name); } echo $document;
Example usage: Shopping list
In the following example, the template of the HTML is defined, with a data-template
attribute to indicate that the LI should be repeated for every item in the data, and a data-bind:text
attribute to set the textContent of the LI to the value of each item in the data.
Note: when binding an array of strings (or Stringable
objects), there is no need to specify a bind key for the data-bind:text
attribute, however in the next example you will see how more complex data structures can be used to bind data at different locations within the template element.
shopping-list.html
:
<!doctype html> <h1>Shopping list</h1> <ul> <li data-template data-bind:text>Item</li> </ul>
PHP used to inject a shopping list
$html = file_get_contents("shopping-list.html"); $document = new \Gt\Dom\HTMLDocument($html); $shoppingList = [ "Pasta", "Rice", "Butter", "Eggs", "Vegetables", ]; $binder = new \Gt\DomTemplate\DocumentBinder($document); $binder->bindList($shoppingList); // this removes the data-bind and data-template attributes: $binder->cleanupDocument(); echo $document;
Output
<!doctype html> <h1>Shopping list</h1> <ul> <li>Pasta</li> <li>Rice</li> <li>Butter</li> <li>Eggs</li> <li>Vegetables</li> </ul>
Advanced usage: bind database results directly to the page
In the following example, the data-template
attribute is used to repeat the LI for every result in the dataset. This dataset represents data that would typically come from a database query or API, but for this example we're simply hard-coding the data.
Within the LI, you can see where the various fields will be bound:
- The surrounding anchor element has its href attribute injected with the user's ID.
- The IMG src and alt attributes have their values injected with the username.
- The IMG alt attribute has the username injected.
- The H2 and H3 elements have their textContent set to the username and type field.
Note that the actual structure of the HTML can be changed at any time, and it's just the data-bind
and use of curly braces that define where the data is bound to.
user-list.html
:
<!doctype html> <h1>Users</h1> <ul> <li data-template> <a href="/user/settings/{{id}}"> <img src="/img/user/{{username}}.jpg" alt="Profile image for {{username}}" /> <h2 data-bind:text="username">username</h2> <h3 data-bind:text="type">user type</h3> </a> </li> </ul>
PHP used to inject the list
// The $data could be from a database, or any other source. // For now, we're just hard-coding the data, so we can see its structure. $data = [ [ "id" => 123, "username" => "codyboy", "type" => "user", ], [ "id" => 456, "username" => "scarlett", "type" => "user", ], [ "id" => 789, "username" => "greg", "type" => "owner", ], ]; $html = file_get_contents("user-list.html"); $document = new \Gt\Dom\HTMLDocument($html); $binder = new \Gt\DomTemplate\DocumentBinder($document); $binder->bindList($data); $binder->cleanupDocument(); echo $document;
Output
<!doctype html> <h1>Users</h1> <ul> <li> <a href="/user/settings/123"> <img src="/img/user/codyboy.jpg" alt="Profile image for codyboy" /> <h2>codyboy</h2> <h3>user</h3> </a> </li> <li> <a href="/user/settings/456"> <img src="/img/user/scarlett.jpg" alt="Profile image for scarlett" /> <h2>scarlett</h2> <h3>user</h3> </a> </li> <li> <a href="/user/settings/789"> <img src="/img/user/greg.jpg" alt="Profile image for greg" /> <h2>greg</h2> <h3>owner</h3> </a> </li> </ul>
Features at a glance
- Binding of dynamic data - bind key value pairs, associative arrays, lists of associative arrays and even nested lists.
- HTML components - organise and reuse DOM trees by storing separate components in their own HTML files, and including them using custom HTML tags.
- Page templates - create partial HTML documents that "extend" others.
- Easily modularise CSS by selecting their custom tag names.