html-template

HTML Template API Specification

Version: 1.0.0

Overview

HTML Template is a templating system that uses HTML5 microdata attributes (itemprop, itemtype, itemscope) for data binding. This specification defines the core API and behavior that implementations should follow regardless of programming language.

Core Concepts

1. Template Definition

A template is an HTML fragment containing elements with microdata attributes that define data binding points.

Microdata Attributes:

Template Attributes:

2. Data Binding Syntax

API Methods

Constructor/Initialization

HtmlTemplate(templateSource, [selector])

Parameters:

Returns: Template instance

Behavior:

Primary Rendering Method

render(data) -> Element | Array<Element>

Parameters:

Returns:

Behavior:

  1. Matches data properties to itemprop attributes
  2. Handles nested objects via itemscope
  3. Processes arrays by cloning template elements
  4. Applies constraints and scopes
  5. Generates itemid attributes using source element’s base URI

Rendering from DOM Elements

renderFromElement(sourceElement) -> Element | Array<Element>

Parameters:

Returns:

Behavior:

Data Processing Rules

1. Property Matching

Properties are matched to elements by:

  1. Direct itemprop attribute matching
  2. Nested object traversal with itemscope
  3. Type matching when itemtype is specified

2. Value Assignment

Standard Elements:

Special Elements:

3. Array Processing

When itemprop="property[]" or data property is an array:

  1. Clone the template element for each array item
  2. Remove the [] suffix when processing each item
  3. Maintain array order in rendered output

4. Attribute Interpolation

Pattern: ${propertyName} in any attribute value

Example:

<a href="${url}" title="${description}">Link</a>

5. Microdata Type Handling

When @type and @context are present in data:

  1. Match elements with corresponding itemtype
  2. Automatically determine rendering root
  3. Apply Schema.org validation if configured

Constraint System

1. Scope Constraints

data-scope="propertyName" - Element renders only when the specified property matches the current context’s @id.

Example:

<li itemtype="https://schema.org/Action" data-scope="agent">

2. Expression Constraints

data-constraint="expression" - Evaluates simple expressions for conditional rendering.

Supported Operators:

Example:

<div data-constraint="status == 'active'">
<div data-constraint="agent == @id">

Form Processing

Form to Data Conversion

Forms can be used as data sources with special handling:

Dot Notation: Form fields with dots in names create nested objects

Array Handling: Multiple inputs with same name create arrays

Checkbox Arrays: Checkboxes with [] suffix

Base URI Resolution

itemid Generation Rules

The itemid attribute is used to reference the authoritative source of microdata. It is only added to rendered (non-authoritative) elements to indicate where the original data came from.

  1. Source Requirement: Only generate itemid when rendering from existing microdata elements that have an id attribute
  2. Non-authoritative Only: Never add itemid to the original/authoritative element itself
  3. Reference Format: itemid contains a URI reference to the authoritative element
  4. Base URI: Use the source element’s base URI, not the rendering document’s base URI
  5. Format: baseURI#id

Example:

<!-- Authoritative source at https://example.com/data/people.html -->
<li id="person123" itemscope itemtype="https://schema.org/Person">
    <span itemprop="name">John Doe</span>
</li>

<!-- Rendered (non-authoritative) copy -->
<div itemscope itemtype="https://schema.org/Person" itemid="https://example.com/data/people.html#person123">
    <h1 itemprop="name">John Doe</h1>
</div>

Key Points:

Error Handling

Implementations should handle:

  1. Missing Properties: Gracefully handle undefined properties
  2. Type Mismatches: Convert or skip incompatible data types
  3. Invalid Templates: Provide clear error messages
  4. Circular References: Detect and prevent infinite loops

Optional Features

1. Validation Modes

2. Caching

3. Custom Handlers

Implementation Notes

Performance Considerations

  1. Use document fragments for efficient DOM manipulation
  2. Minimize template re-parsing
  3. Cache compiled templates when possible
  4. Batch DOM operations

Security

  1. Sanitize user-provided data to prevent XSS
  2. Validate URLs in href and src attributes
  3. Escape HTML entities in text content
  4. Never execute arbitrary JavaScript from templates

Compliance Levels

Level 1: Core

Level 2: Advanced

Level 3: Full

Version History