Template

A template helper for resolving, cloning and preparing reusable DOM templates.

Import
the javascript logo
import { Template } from "@schukai/monster/source/dom/template.mjs";
Source
the git logo
Package
the npm logo
Since
1.6.0

Introduction

Template wraps an existing <template> element and gives Monster a predictable way to resolve and clone reusable markup.

Core Responsibilities

SurfaceWhat it solves
new Template(template)Wraps a concrete template node in a stable helper object.
createDocumentFragment()Clones the template content without mutating the original source.
findDocumentTemplate(id, node)Resolves template IDs with theme and prefix support from the surrounding document tree.

Where It Is Used

Template is a foundational helper for CustomElement, themed markup, repeated insertions and any component that wants reusable HTML without hand-written clone logic.

What Developers Usually Miss

  • Theme lookup: findDocumentTemplate() prefers themed IDs such as my-template-myTheme.
  • Prefix lookup: a host can scope template IDs with data-monster-template-prefix.
  • Safe reuse: every call returns a fresh fragment, so the source template remains unchanged.

Clone A Named Template Fragment

import { Template } from "@schukai/monster/source/dom/template.mjs";

const templateElement = document.getElementById("template-demo");
const target = document.getElementById("template-target");

const template = new Template(templateElement);
target.append(template.createDocumentFragment());
Open in playground

Resolve Theme Specific Templates By Id Suffix

import { findDocumentTemplate } from "@schukai/monster/source/dom/template.mjs";

document.documentElement.setAttribute("data-monster-theme-name", "docs");

document.getElementById("template-theme-run").addEventListener("click", () => {
    const template = findDocumentTemplate("message-template");
    const output = document.getElementById("template-theme-output");
    output.replaceChildren(template.createDocumentFragment());
});
Open in playground

Resolve Prefixed Templates Near The Current Node

import { findDocumentTemplate } from "@schukai/monster/source/dom/template.mjs";

const button = document.getElementById("template-prefix-run");
const output = document.getElementById("template-prefix-output");

button.addEventListener("click", () => {
    const template = findDocumentTemplate("banner-panel", button.closest("[data-monster-template-prefix]"));
    output.replaceChildren(template.createDocumentFragment());
});
Open in playground

Clone The Same Template Multiple Times Without Mutating The Source

import { findDocumentTemplate } from "@schukai/monster/source/dom/template.mjs";

document.getElementById("template-repeat-run").addEventListener("click", () => {
    const template = findDocumentTemplate("template-repeat-card");
    const output = document.getElementById("template-repeat-output");
    output.innerHTML = "";

    for (const title of ["Overview", "Design", "Usage"]) {
        const fragment = template.createDocumentFragment();
        fragment.querySelector('[data-role="title"]').textContent = title;
        output.appendChild(fragment);
    }
});
Open in playground

Exported

Template

Derived from

Base

Options

The Options listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the Base.

Option
Type
Default
Description
-/-

Properties

The Properties listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Properties from these parent classes. If you cannot find a specific Properties in this list, we recommend consulting the documentation of the Base.

Methods

The methods listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the Base.

Constructor

constructor(template)
Parameters
  • template {htmltemplateelement}: template
Throws
  • {TypeError} value is not an instance of
  • {TypeError} value is not a function
  • {Error} the function is not defined

Behavioral methods

createDocumentFragment()
Returns
  • {DocumentFragment}
Throws
  • {TypeError} value is not an instance of

Structural methods

getTemplateElement()
Returns
  • {HTMLTemplateElement}

Static methods

[instanceSymbol]()2.1.0
Returns
  • {symbol}
This method is called by the instanceof operator.

Events

This component does not fire any public events. It may fire events that are inherited from its parent classes.

The current width of the area is too small to display the content correctly.