HTML templates and reusable fragments
Quick answer
The HTML template element stores a fragment for later use. Its content is not displayed as ordinary page children until instantiated.
How it works
Use template.content to access the DocumentFragment. For ordinary static markup, clone it deeply with template.content.cloneNode(true) or use document.importNode(template.content, true) to clone into the current document, particularly when custom-element construction context matters. Append the new fragment to a target; inserting a fragment moves its children. Populate dynamic text with textContent rather than interpolating untrusted strings into innerHTML. Repeated clones can duplicate IDs; use classes or unique identifiers. Attach behavior after cloning because addEventListener registrations do not clone.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const t=document.createElement("template");t.innerHTML="<span>ready</span>";document.body.append(t);console.log(t.content.querySelector("span").textContent,document.querySelector("span")===null);})()Expected console output:
ready trueTemplate content is not part of the ordinary rendered document tree.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const t=document.createElement("template");t.innerHTML="<p></p>";for(const label of ["A","<B>"]){const copy=document.importNode(t.content,true);copy.querySelector("p").textContent=label;document.body.append(copy);}console.log(JSON.stringify([...document.querySelectorAll("p")].map(x=>x.textContent)),t.content.childElementCount);})()Expected console output:
["A","<B>"] 1Each clone is independent and the original retains its child. The label is literal text.
Common mistakes
Do not append the original template content repeatedly: the first append empties the fragment. Never parse user-supplied markup just to fill text.
Practice
Create a static template for a list row, instantiate two rows and safely fill their labels.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML templates and reusable fragments — Knowledge Check
Apply the concept and reason about local example output.
Where are a template’s ordinary markup children stored?
Why clone or import a template fragment before insertion?
What is a safe way to fill a cloned label from untrusted text?
What is the expected console output of HTML templates and reusable fragments example 1?
(async () => {const t=document.createElement("template");t.innerHTML="<span>ready</span>";document.body.append(t);console.log(t.content.querySelector("span").textContent,document.querySelector("span")===null);})()What is the expected console output of HTML templates and reusable fragments example 2?
(async () => {const t=document.createElement("template");t.innerHTML="<p></p>";for(const label of ["A","<B>"]){const copy=document.importNode(t.content,true);copy.querySelector("p").textContent=label;document.body.append(copy);}console.log(JSON.stringify([...document.querySelectorAll("p")].map(x=>x.textContent)),t.content.childElementCount);})()