HTMLTemplate is a powerful, microdata-based HTML templating system that bridges the gap between semantic HTML and dynamic data rendering. It leverages HTML5 microdata attributes (itemprop
, itemtype
, itemscope
) to create templates that are both machine-readable and human-maintainable.
For a language-agnostic specification of the template format and behavior, see the Template Specification.
Uses standard HTML5 microdata attributes for data binding, making templates SEO-friendly and semantically meaningful.
Automatic template selection based on Schema.org types, enabling polymorphic rendering.
itemscope
${variable}
syntaxdata-scope
attributedata-constraint
Pure JavaScript implementation with no external dependencies, using ES modules.
Unlike traditional templating engines that use custom syntax, HTMLTemplate uses standard HTML5 microdata attributes. This means:
Templates work as static HTML and can be progressively enhanced with dynamic data. This approach ensures:
By leveraging Schema.org types, HTMLTemplate provides a form of “type safety” for your templates:
<template id="person-template">
<div itemscope itemtype="https://schema.org/Person">
<h1 itemprop="name"></h1>
<p>Email: <a href="mailto:${email}" itemprop="email"></a></p>
<ul>
<li itemprop="skills[]"></li>
</ul>
</div>
</template>
<script type="module">
import { HTMLTemplate } from 'https://jamesaduncan.github.io/html-template/index.mjs';
const template = new HTMLTemplate(document.getElementById('person-template'));
const element = template.render({
"@type": "Person",
"@context": "https://schema.org",
"name": "Jane Doe",
"email": "jane@example.com",
"skills": ["JavaScript", "HTML", "CSS"]
});
document.body.appendChild(element);
</script>
HTMLTemplate is built on these core principles:
HTMLTemplate works in all modern browsers that support:
No polyfills or transpilation required for modern browsers.
This project is open source. See the LICENSE file for details.