<template>
Tag in HTML: A Comprehensive GuideThe <template>
tag in HTML is a powerful yet often underutilized feature that allows developers to define reusable HTML fragments without rendering them immediately. This guide will take you through every aspect of the <template>
tag, including its syntax, functionality, real-world use cases, and advanced techniques.
By the end of this article, you’ll have a deep understanding of how to leverage <template>
for creating dynamic and efficient web applications.
<template>
Tag?The <template>
tag is a container for HTML code that is not rendered in the document until it is explicitly used via JavaScript. This makes it perfect for dynamically injecting UI components, improving performance, and maintaining clean code structure.
<template>
is not rendered when the page loads.A <template>
element consists of an opening and closing tag enclosing any HTML content that should be prepared for later use.
<template>
<div class="user-card">
<h2>User Name</h2>
<p>Email: user@example.com</p>
</div>
</template>
This markup won’t be displayed on the page until JavaScript explicitly processes it.
<template>
TagTo utilize a <template>
, you need JavaScript to extract and insert its contents into the DOM.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Template Example</title>
</head>
<body>
<template id="user-template">
<div class="user-card">
<h2></h2>
<p>Email: <span></span></p>
</div>
</template>
<div id="user-container"></div>
<script>
const users = [
{ name: "John Doe", email: "john@example.com" },
{ name: "Jane Smith", email: "jane@example.com" },
];
const container = document.getElementById("user-container");
const template = document.getElementById("user-template");
users.forEach((user) => {
const clone = template.content.cloneNode(true);
clone.querySelector("h2").textContent = user.name;
clone.querySelector("span").textContent = user.email;
container.appendChild(clone);
});
</script>
</body>
</html>
<template>
contains a user card structure.<template>
Instead of manually inserting multiple elements, <template>
can simplify the process.
<template id="product-template">
<div class="product">
<h3></h3>
<p>Price: $<span></span></p>
</div>
</template>
<div id="product-list"></div>
<script>
const products = [
{ name: "Laptop", price: 1200 },
{ name: "Phone", price: 700 },
];
const productList = document.getElementById("product-list");
const productTemplate = document.getElementById("product-template");
products.forEach((product) => {
const clone = productTemplate.content.cloneNode(true);
clone.querySelector("h3").textContent = product.name;
clone.querySelector("span").textContent = product.price;
productList.appendChild(clone);
});
</script>
You can use <template>
for modals, popups, and other UI elements that shouldn’t be in the DOM until needed.
<button onclick="showModal()">Open Modal</button>
<template id="modal-template">
<div class="modal">
<h2>Modal Title</h2>
<p>This is a modal content.</p>
<button onclick="closeModal()">Close</button>
</div>
</template>
<script>
function showModal() {
const modalTemplate = document.getElementById("modal-template");
const modal = modalTemplate.content.cloneNode(true);
document.body.appendChild(modal);
}
function closeModal() {
document.querySelector(".modal").remove();
}
</script>
<slot>
Inside <template>
When combining <template>
with Web Components, <slot>
placeholders allow greater flexibility.
<template id="alert-template">
<div class="alert">
<slot></slot>
</div>
</template>
Templates can be nested inside each other for complex UI structures.
<template id="parent-template">
<div>
<h1>Parent</h1>
<template>
<p>Nested Template</p>
</template>
</div>
</template>
content.cloneNode(true)
.The <template>
tag is a hidden gem in HTML that enables efficient, reusable, and performant web development. By leveraging its power, you can optimize your JavaScript-driven UI components and keep your code clean and maintainable.