-
-
Notifications
You must be signed in to change notification settings - Fork 3
Getting started
In this section we will build the smallest useful DomTemplate setup from scratch.
The main thing to know is that DocumentBinder is the public entry point, but it relies on a few smaller helper objects behind the scenes. Once those are wired together, we can bind values, lists, tables, components, and partials in a consistent way.
Tip
In WebEngine, you usually do not instantiate these objects yourself. The framework builds and injects them for you.
composer require phpgt/domtemplateuse Gt\Dom\HTMLDocument;
$html = <<<'HTML'
<!doctype html>
<h1>Hello, <span data-bind:text="name">you</span>!</h1>
HTML;
$document = new HTMLDocument($html);DomTemplate works with HTMLDocument from phpgt/dom, so this is always our starting point.
Each piece of functionality within DomTemplate is controlled by a separate class. Here we'll build up the dependency tree, so all classes are aware of each other in a way that respects object oriented encapsulation:
use GT\DomTemplate\BindableCache;
use GT\DomTemplate\DocumentBinder;
use GT\DomTemplate\ElementBinder;
use GT\DomTemplate\HTMLAttributeBinder;
use GT\DomTemplate\HTMLAttributeCollection;
use GT\DomTemplate\ListBinder;
use GT\DomTemplate\ListElementCollection;
use GT\DomTemplate\PlaceholderBinder;
use GT\DomTemplate\TableBinder;
$elementBinder = new ElementBinder();
$placeholderBinder = new PlaceholderBinder();
$tableBinder = new TableBinder();
$listBinder = new ListBinder();
$listElementCollection = new ListElementCollection($document);
$bindableCache = new BindableCache();
$htmlAttributeBinder = new HTMLAttributeBinder();
$htmlAttributeCollection = new HTMLAttributeCollection();
$elementBinder->setDependencies(
$htmlAttributeBinder,
$htmlAttributeCollection,
$placeholderBinder,
);
$htmlAttributeBinder->setDependencies($listBinder, $tableBinder);
$listBinder->setDependencies(
$elementBinder,
$listElementCollection,
$bindableCache,
$tableBinder,
);
$tableBinder->setDependencies(
$listBinder,
$listElementCollection,
$elementBinder,
$htmlAttributeBinder,
$htmlAttributeCollection,
$placeholderBinder,
);
$binder = new DocumentBinder($document);
$binder->setDependencies(
$elementBinder,
$placeholderBinder,
$tableBinder,
$listBinder,
$listElementCollection,
$bindableCache,
);That looks like quite a lot at first glance, but we only have to set it up once. After that, all normal binding goes through $binder.
$binder->bindKeyValue("name", "Cody");At this point the document contains the updated text, but it still contains the original DomTemplate attributes too.
$binder->cleanupDocument();
echo $document;cleanupDocument() removes helper attributes such as data-bind, data-list, data-template, and data-element from the output. It also removes unbound optional elements marked with data-element.
Output HTML:
<!doctype html>
<h1>Hello, <span>Cody</span>!</h1>Most of the time, DocumentBinder is the right tool.
- Use
DocumentBinderwhen working on the whole page. - Use
ComponentBinderwhen working inside a single expanded component.
ComponentBinder has the same binding methods, but it automatically limits the bind scope to one component element.
Note
In WebEngine, you can just use Binder instead of ComponentBinder or DocumentBinder and you'll be given the correct instance depending on the context in which you're using it.
Within a page's PHP you'll get the DocumentBinder and within a component's PHP you'll get the ComponentBinder.
If we are also using component files or partial templates, there are two extra helpers:
-
ComponentExpanderexpands custom HTML elements from a component directory. -
PartialExpanderapplies a page template from a partial directory.
Those are covered properly later in HTML components and Partials.
Our binder is now wired up. Next we can learn the core public API in Binding basics.
PHP.GT/DomTemplate is a separately maintained component of PHP.GT/WebEngine.