← Back to HTML & CSS, page 6

html-css-0112 · Medium mid

What is the template element, and how do you use it to build repeated markup?

A template holds inert HTML in a DocumentFragment: nothing renders, runs or loads. Clone template.content with cloneNode(true), fill it using textContent and add it to the page. It is cleaner and safer than gluing HTML strings.

Full answer

A template element holds HTML that is not shown and not run. Its content is parsed but kept inert in a separate document fragment. Images inside do not load, scripts inside do not run, and styles inside do not apply to the page. A normal querySelector on the page does not find elements inside it.

To use it, read template.content, which is a DocumentFragment. Call content.cloneNode(true) to make a copy, fill the copy with data and add it to the page. Each clone is independent, so you can repeat it for every row of data.

Why not build strings with innerHTML?

  • A template is real HTML, so it is easy to read and edit, and editors can check it.
  • Setting textContent on parts of the clone puts user text in as plain text and avoids script injection problems that happen when you glue user input into an HTML string.
  • It is faster than parsing the same long string again for each item.

Compared with a div that is hidden with CSS, a template's content is never rendered and never fetches images.

Templates are also the basis of custom elements with shadow DOM, where a template is cloned into the shadow root.

The page shows two list items with the names. Searching the page for .item finds only the two clones, and not the template's own list item. The image in the template is never requested until a clone is added, and the clone then requests it.

Always set text with textContent, and not with an HTML string, when the data comes from users.

html
<ul id="list"></ul>
<template id="row">
  <li class="item"><strong class="name"></strong> <img class="pic" src="/never-loaded.png" alt=""></li>
</template>
<script>
  const row = document.getElementById("row");
  for (const n of ["Asha", "Ravi"]) {
    const copy = row.content.cloneNode(true);
    copy.querySelector(".name").textContent = n;
    document.getElementById("list").append(copy);
  }
</script>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Template content is inert and not part of the page tree.
  • Use content.cloneNode(true) to make copies.
  • Fill clones with textContent for safety.
  • Each clone is independent.
  • Templates are used by custom elements.

Common mistakes

  • Looking for elements inside a template with document.querySelector.
  • Forgetting cloneNode(true), which moves the content instead of copying it.
  • Filling clones with innerHTML and user text.

Follow-ups

  • Why does an image in a template not load?
  • What does cloneNode(true) do differently from cloneNode(false)?

Tips

  • Explain inert content and the DocumentFragment in one sentence.
  • Mention textContent as the safe way to insert text.