Template
A template helper for resolving, cloning and preparing reusable DOM templates.
import { Template } from "@schukai/monster/source/dom/template.mjs";Introduction
Template wraps an existing <template> element and gives Monster a predictable way to resolve and clone reusable markup.
Core Responsibilities
| Surface | What 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 asmy-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());Template Result
This node was cloned through Monster Template.
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());
});Default template
Theme specific template
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());
});Prefixed Template
This template was resolved through the surrounding prefix.
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);
}
});Exported
TemplateDerived from
BaseOptions
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.
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)template{htmltemplateelement}: template
{TypeError}value is not an instance of{TypeError}value is not a function{Error}the function is not defined
Behavioral methods
createDocumentFragment()- {DocumentFragment}
{TypeError}value is not an instance of
Structural methods
getTemplateElement()- {HTMLTemplateElement}
Static methods
[instanceSymbol]()2.1.0- {symbol}
instanceof operator.Events
This component does not fire any public events. It may fire events that are inherited from its parent classes.