HTMLTemplate supports multiple data sources, allowing you to render templates from various inputs.
The most common data source is plain JavaScript objects.
const data = {
title: "Welcome",
message: "Hello, World!",
timestamp: "2024-01-15T10:30:00Z"
};
const element = template.render(data);
const data = {
product: {
name: "Laptop",
specs: {
cpu: "Intel i7",
ram: "16GB",
storage: "512GB SSD"
}
}
};
const data = {
shoppingCart: {
items: [
{ name: "Book", price: 15.99, quantity: 2 },
{ name: "Pen", price: 1.99, quantity: 5 }
],
total: 41.93
}
};
Include @type
and @context
for type-based template matching:
const data = {
"@type": "Person",
"@context": "https://schema.org",
"@id": "john-doe",
"name": "John Doe",
"email": "john@example.com",
"jobTitle": "Software Engineer"
};
Render multiple items using arrays.
const data = [
{ name: "Item 1", value: 100 },
{ name: "Item 2", value: 200 },
{ name: "Item 3", value: 300 }
];
const elements = template.render(data); // Returns array of elements
const data = [
{
"@type": "Person",
"@context": "https://schema.org",
"name": "Alice"
},
{
"@type": "Organization",
"@context": "https://schema.org",
"name": "Acme Corp"
}
];
Different types can coexist in arrays:
const data = [
{
"@type": "BlogPosting",
"@context": "https://schema.org",
"headline": "New Product Launch",
"datePublished": "2024-01-15"
},
{
"@type": "NewsArticle",
"@context": "https://schema.org",
"headline": "Breaking News",
"datePublished": "2024-01-16"
}
];
Extract data from existing HTML with microdata attributes.
<!-- Source HTML -->
<div id="person-data" itemscope itemtype="https://schema.org/Person">
<span itemprop="name">Jane Smith</span>
<span itemprop="email">jane@example.com</span>
<div itemprop="address" itemscope>
<span itemprop="streetAddress">123 Main St</span>
<span itemprop="addressLocality">Boston</span>
<span itemprop="addressRegion">MA</span>
</div>
</div>
<script>
// Extract and render
const sourceElement = document.getElementById('person-data');
const rendered = template.render(sourceElement);
</script>
<!-- Source HTML -->
<ul id="people-list">
<li itemscope itemtype="https://schema.org/Person">
<span itemprop="name">John Doe</span>
<span itemprop="email">john@example.com</span>
</li>
<li itemscope itemtype="https://schema.org/Person">
<span itemprop="name">Jane Doe</span>
<span itemprop="email">jane@example.com</span>
</li>
</ul>
<script>
// Extract from list
const list = document.getElementById('people-list');
const elements = template.render(list); // Returns array
</script>
@type
and @context
@id
<article id="blog-post"
itemscope
itemtype="https://schema.org/BlogPosting">
<h1 itemprop="headline">Understanding Microdata</h1>
<div itemprop="author"
itemscope
itemtype="https://schema.org/Person">
<span itemprop="name">Dr. Sarah Johnson</span>
<span itemprop="email">sarah@university.edu</span>
</div>
<time itemprop="datePublished"
datetime="2024-01-15">January 15, 2024</time>
<div itemprop="articleBody">
<p>Microdata provides a way to label content...</p>
</div>
<!-- Multiple values create array -->
<meta itemprop="keywords" content="microdata">
<meta itemprop="keywords" content="structured data">
<meta itemprop="keywords" content="SEO">
</article>
Extracted data:
{
"@type": "BlogPosting",
"@context": "https://schema.org",
"@id": "blog-post",
"headline": "Understanding Microdata",
"author": {
"@type": "Person",
"@context": "https://schema.org",
"name": "Dr. Sarah Johnson",
"email": "sarah@university.edu"
},
"datePublished": "January 15, 2024",
"articleBody": "Microdata provides a way to label content...",
"keywords": ["microdata", "structured data", "SEO"]
}
Extract data from forms with automatic handling of nested properties and arrays.
<form id="contact-form">
<input name="name" value="John Doe">
<input name="email" value="john@example.com">
<textarea name="message">Hello!</textarea>
<select name="category">
<option value="support" selected>Support</option>
<option value="sales">Sales</option>
</select>
</form>
<script>
const form = document.getElementById('contact-form');
const data = template.render(form);
// Result: {
// name: "John Doe",
// email: "john@example.com",
// message: "Hello!",
// category: "support"
// }
</script>
<form id="user-form">
<input name="user.firstName" value="Jane">
<input name="user.lastName" value="Smith">
<input name="user.contact.email" value="jane@example.com">
<input name="user.contact.phone" value="555-1234">
</form>
<script>
const form = document.getElementById('user-form');
const data = template.render(form);
// Result: {
// user: {
// firstName: "Jane",
// lastName: "Smith",
// contact: {
// email: "jane@example.com",
// phone: "555-1234"
// }
// }
// }
</script>
<form id="items-form">
<!-- Using [] for arrays -->
<input name="tags[]" value="javascript">
<input name="tags[]" value="html">
<input name="tags[]" value="css">
<!-- Using numeric indices -->
<input name="items[0].name" value="Product A">
<input name="items[0].price" value="10.99">
<input name="items[1].name" value="Product B">
<input name="items[1].price" value="20.99">
</form>
<script>
const form = document.getElementById('items-form');
const data = template.render(form);
// Result: {
// tags: ["javascript", "html", "css"],
// items: [
// { name: "Product A", price: "10.99" },
// { name: "Product B", price: "20.99" }
// ]
// }
</script>
<form id="order-form">
<!-- Customer info -->
<fieldset>
<legend>Customer Information</legend>
<input name="customer.name" placeholder="Full Name">
<input name="customer.email" type="email" placeholder="Email">
<input name="customer.phone" type="tel" placeholder="Phone">
</fieldset>
<!-- Shipping address -->
<fieldset>
<legend>Shipping Address</legend>
<input name="shipping.street" placeholder="Street Address">
<input name="shipping.city" placeholder="City">
<input name="shipping.state" placeholder="State">
<input name="shipping.zip" placeholder="ZIP Code">
</fieldset>
<!-- Order items -->
<fieldset>
<legend>Items</legend>
<div class="item">
<input name="items[0].product" value="Widget">
<input name="items[0].quantity" type="number" value="2">
<input name="items[0].price" type="number" value="9.99">
</div>
<div class="item">
<input name="items[1].product" value="Gadget">
<input name="items[1].quantity" type="number" value="1">
<input name="items[1].price" type="number" value="19.99">
</div>
</fieldset>
<!-- Options -->
<fieldset>
<legend>Options</legend>
<label>
<input type="checkbox" name="options.giftWrap" value="true">
Gift Wrap
</label>
<label>
<input type="checkbox" name="options.expressShipping" value="true" checked>
Express Shipping
</label>
</fieldset>
</form>
You can also pass a FormData object directly:
const formData = new FormData(document.getElementById('my-form'));
const rendered = template.render(formData);
You can combine data from multiple sources:
// Extract from microdata
const microdataElement = document.querySelector('[itemscope]');
const extractedData = template.render(microdataElement);
// Enhance with additional data
const enhancedData = {
...extractedData,
lastUpdated: new Date().toISOString(),
status: "published"
};
// Render with enhanced data
const finalElement = template.render(enhancedData);