-
-
Notifications
You must be signed in to change notification settings - Fork 3
Overview
DomTemplate falls into three ideas:
- bind data into HTML,
- repeat bits of HTML from lists of data,
- compose whole pages from reusable pieces.
In this guide we will build those ideas up one at a time, starting with the smallest useful examples and then gradually moving into the more advanced behaviour.
<p>Welcome back, <span data-bind:text="firstName">friend</span>!</p>
<input name="email" data-bind:value="email" />$binder->bindData([
"firstName" => "Ada",
"email" => "ada@example.com",
]);Here we can see the basic pattern of the library: the HTML already contains sensible defaults, and the binder only replaces the pieces we have marked.
<ul>
<li data-list data-bind:text>Item</li>
</ul>$binder->bindList([
"Tea",
"Milk",
"Biscuits",
]);If we do this, the original <li> is treated as a template and cloned once for each list item.
<a href="/user/{{id}}">View {{name ?? this user}}</a>$binder->bindData([
"id" => 210,
"name" => "Cody",
]);This is handy when we only want to replace part of an attribute or bit of text rather than the whole property.
<profile-summary />If there is a matching component file, DomTemplate will replace the contents of the custom element with that component's HTML.
<!--
extends=base-page
[vars]
title=Orders
-->
<h1>Orders</h1>
<ul>
<li data-list>
<order-item data-bind:id="id" />
</li>
</ul>A partial template allows us to provide reusable outer page layout in a flexible way that can be extended in other pages.
Note
In WebEngine applications, the use of _header.html and _footer.html files might be simple enough for your page template needs - consider using this before reaching for partial page templates - it might be a simpler solution that works for you.
Now that we know what the library is for, move on to getting started so we can wire up a working binder.
PHP.GT/DomTemplate is a separately maintained component of PHP.GT/WebEngine.