A powerful, microdata-based HTML templating system that uses standard HTML5 attributes for data binding. HTMLTemplate bridges the gap between semantic HTML and dynamic data rendering without introducing new syntax or dependencies.
itemprop
, itemtype
, itemscope
)${variable}
syntax for dynamic attributesimport { HTMLTemplate } from 'https://jamesaduncan.github.io/html-template/index.mjs';
// Define your template
const template = new HTMLTemplate(document.getElementById('my-template'));
// Render with data
const element = template.render({
name: "John Doe",
email: "john@example.com",
skills: ["JavaScript", "HTML", "CSS"]
});
document.body.appendChild(element);
<template id="my-template">
<div itemscope>
<h1 itemprop="name"></h1>
<p>Email: <a href="mailto:${email}" itemprop="email"></a></p>
<ul>
<li itemprop="skills[]"></li>
</ul>
</div>
</template>
Comprehensive documentation is available in the docs directory:
Unlike traditional templating engines that introduce custom syntax, HTMLTemplate uses standard HTML5 microdata attributes. Your templates are valid HTML that can be indexed by search engines and understood by machines.
Templates work as static HTML and can be progressively enhanced with dynamic data. This ensures better SEO, graceful degradation, and improved accessibility.
By leveraging Schema.org types, HTMLTemplate provides a form of โtype safetyโ for your templates, automatically matching data to the correct template based on type information.
HTMLTemplate is a single ES module file. Simply include it in your project:
import { HTMLTemplate } from 'https://jamesaduncan.github.io/html-template/index.mjs';
Or use it directly in HTML:
<script type="module">
import { HTMLTemplate } from 'https://jamesaduncan.github.io/html-template/index.mjs';
// Your code here
</script>
Run the test suite by serving the files with any HTTP server:
npx http-server -o /tests/
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open source and available under the MIT License.
<template id="card">
<div itemscope class="card">
<h2 itemprop="title"></h2>
<p itemprop="description"></p>
<time itemprop="date"></time>
</div>
</template>
const template = new HTMLTemplate(document.getElementById('card'));
const card = template.render({
title: "Welcome to HTMLTemplate",
description: "A microdata-based templating system",
date: "2024-01-15"
});
<template id="mixed">
<!-- Person template -->
<div itemscope itemtype="https://schema.org/Person">
<h2 itemprop="name"></h2>
<p itemprop="jobTitle"></p>
</div>
<!-- Organization template -->
<div itemscope itemtype="https://schema.org/Organization">
<h2 itemprop="name"></h2>
<p itemprop="description"></p>
</div>
</template>
const template = new HTMLTemplate(document.getElementById('mixed'));
// Automatically uses Person template
const person = template.render({
"@type": "Person",
"@context": "https://schema.org",
"name": "Jane Doe",
"jobTitle": "Software Engineer"
});
// Automatically uses Organization template
const org = template.render({
"@type": "Organization",
"@context": "https://schema.org",
"name": "Acme Corp",
"description": "We make everything"
});
For more examples, see the documentation.
Made with โค๏ธ using standard web technologies