The main class for creating and rendering templates.
new HTMLTemplate(templateElement, selector)
Creates a new HTMLTemplate instance.
HTMLTemplateElement
(required) - The HTML template element to usestring
(optional) - CSS selector to find the root element within the templateError
- If templateElement is not an HTMLTemplateElement// Basic usage
const template = new HTMLTemplate(document.getElementById('my-template'));
// With selector
const template = new HTMLTemplate(
document.getElementById('my-template'),
'.content'
);
Renders data using the template.
Object|Array|Element|HTMLFormElement
- The data to renderElement|Element[]
- Rendered DOM element(s). Returns an array when input is an array.// Render single object
const element = template.render({
name: "John Doe",
email: "john@example.com"
});
// Render array
const elements = template.render([
{ name: "John" },
{ name: "Jane" }
]);
// Render from DOM element
const sourceElement = document.querySelector('[itemscope]');
const rendered = template.render(sourceElement);
// Render from form
const form = document.getElementById('my-form');
const formRendered = template.render(form);
Standard JavaScript objects with properties matching itemprop
attributes:
template.render({
title: "Hello",
description: "World",
tags: ["one", "two", "three"]
});
Arrays of objects for rendering multiple items:
template.render([
{ name: "Item 1", value: 10 },
{ name: "Item 2", value: 20 },
{ name: "Item 3", value: 30 }
]);
Elements containing microdata attributes:
// Source HTML
<div itemscope itemtype="https://schema.org/Person">
<span itemprop="name">Jane Doe</span>
<span itemprop="email">jane@example.com</span>
</div>
// Usage
const sourceElement = document.querySelector('[itemscope]');
const rendered = template.render(sourceElement);
Forms with automatic data extraction:
// Form HTML
<form id="person-form">
<input name="name" value="John Doe">
<input name="email" value="john@example.com">
<input name="address.street" value="123 Main St">
<input name="address.city" value="Anytown">
</form>
// Usage
const form = document.getElementById('person-form');
const rendered = template.render(form);
// Extracts: {
// name: "John Doe",
// email: "john@example.com",
// address: {
// street: "123 Main St",
// city: "Anytown"
// }
// }
Templates can automatically match based on Schema.org types:
// Template with type
<div itemscope itemtype="https://schema.org/Person">
<h1 itemprop="name"></h1>
</div>
// Data with @type
template.render({
"@type": "Person",
"@context": "https://schema.org",
"name": "John Doe"
});
@type
but no @context
is treated as untyped@type
and @context
, and template has matching itemtype
, they are pairedImportant: Always include both @type
and @context
together:
// ✅ Correct
{
"@type": "Person",
"@context": "https://schema.org",
"name": "John Doe"
}
// ❌ Incorrect - @type without @context
{
"@type": "Person",
"name": "John Doe"
}
When extracting from DOM elements:
itemtype
→ @type
and @context
id
attribute → @id
itemscope
→ nested objectsitemprop
→ arraysperson.name
→ { person: { name: "value" } }
items[]
→ { items: ["value1", "value2"] }
items[0]
→ { items: ["value"] }
Variables in attributes use ${name}
syntax:
<a href="${url}" title="${description}">Link</a>
Only simple property names are supported (no expressions).
itemprop="property[]"
are cloned for each array item[]
notation is removed from the final outputElement | Property Set | Notes |
---|---|---|
<input type="text"> |
value attribute |
Also sets value property |
<input type="checkbox"> |
checked property |
Sets/removes checked attribute |
<input type="radio"> |
checked property |
Sets/removes checked attribute |
<textarea> |
textContent and value |
Both are set |
<select> |
selected on options |
Finds matching option by value |
<option> |
selected property |
Sets/removes selected attribute |
<meta> |
content attribute |
For metadata |
<img> |
src attribute |
For images |
<audio> , <video> |
src attribute |
For media |
<time> |
datetime attribute |
For temporal data |
<output> |
value property |
For form output |
Others | textContent |
Default behavior |
HTMLTemplate logs warnings for:
false
null
)Templates are parsed once during construction and cached:
data-scope
instead of complex constraints when possible