Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
PHP library for binding dynamic data to reusable HTML components using data attributes and templates.
DomTemplate is an open-source PHP library that binds dynamic data to HTML documents using data attributes, custom-element templates, and reusable HTML components, built on the PHP.GT Dom library.
DomTemplate is a PHP data-binding engine that takes an HTML document and a PHP data structure — key-value pairs, arrays, lists of associative arrays, or nested lists — and injects the data into the document via data-bind and data-template attributes. It outputs the fully rendered HTML with the binding markup removed when you call cleanupDocument(). The library is developed by the PhpGt project, runs in any PHP environment with the DOM extension, and is distributed as a Composer package.
data-bind:text attribute to replace an element's text content with a single value, as shown in the "Hello, you!" greeter example.data-template and bind an array of strings or objects to automatically repeat that element once per item.href, src, and alt using curly-brace placeholders like {{id}}, demonstrated in the user-list example.cleanupDocument() removes all data-bind and data-template attributes after data injection, leaving clean HTML output.data-template list item to generate a complete shopping list or menu.data-bind:text after the page loads.The workflow starts with loading an HTML file into a GT\Dom\HTMLDocument object, then instantiating a GT\DomTemplate\DocumentBinder with that document. You call bindKeyValue() to inject a single value, bindList() to repeat a template over a data array, or more targeted bind methods for nested data. Finally, cleanupDocument() strips all binding attributes so the echoed HTML is clean.
DomTemplate is an open-source library published on GitHub under the PhpGt project and available through Packagist. The project is sponsored by JetBrains as part of its open-source support program, and no paid tier or license fee is mentioned.
No. DomTemplate is a standalone PHP library that works with the DOM extension built into PHP. It does not depend on any specific framework, so it can be added to plain PHP applications or integrated into existing MVC setups via Composer.
Yes. The user-list example shows attribute injection into href, src, and alt using curly-brace placeholders such as {{id}} within the markup. When binding a list of associative arrays, each named field can populate its corresponding placeholder in the attribute.
cleanupDocument() permanently removes the data-bind and data-template attributes from the rendered DOM. This produces a clean HTML document with no leftover template markup, ready for output to the browser. After cleanup, the document cannot be re-bound with the same template.
