html-template

HTMLTemplate

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.

โœจ Features

๐Ÿš€ Quick Start

import { 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>

๐Ÿ“š Documentation

Comprehensive documentation is available in the docs directory:

๐Ÿ’ก Why HTMLTemplate?

Standards-Based

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.

Progressive Enhancement

Templates work as static HTML and can be progressively enhanced with dynamic data. This ensures better SEO, graceful degradation, and improved accessibility.

Type Safety

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.

๐Ÿ› ๏ธ Installation

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>

๐Ÿงช Testing

Run the test suite by serving the files with any HTTP server:

npx http-server -o /tests/

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

๐Ÿ“„ License

This project is open source and available under the MIT License.

๐ŸŒŸ Examples

Simple Object Rendering

<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"
});

Type-Based Rendering

<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