DEV Community

Rijul Rajesh
Rijul Rajesh

Posted on

Underrated HTML gems: How <template> is a game changer!

Have you ever wanted to create HTML content dynamically without cluttering your code with hidden elements? Well, let me introduce you to one of HTML's most underrated features: the <template> element.

What is the <template> Element?

The <template> element is like a secret stash for your HTML content. It allows you to define a block of HTML that isn't rendered immediately when the page loads. Instead, it stays hidden until you decide to bring it to life using JavaScript.

Think of it as a pre-packaged UI component that you can clone and insert into your webpage whenever needed.

Basic Syntax

Here's what a simple template looks like:

<template id="user-card-template">
  <div class="user-card">
    <h3 class="name"></h3>
    <p class="email"></p>
  </div>
</template>
Enter fullscreen mode Exit fullscreen mode

This chunk of HTML won't appear on the page until you explicitly add it using JavaScript.

Why Use <template>?

Using <template> offers several advantages:

  • Performance Boost: It prevents unnecessary DOM elements from loading initially.
  • Code Organization: Keeps your HTML cleaner by avoiding hidden elements inside the main document.
  • Reusability: You can clone and reuse the same structure multiple times.

Practical Applications

Now, let’s get our hands dirty with some real-world use cases!

1. Generating Dynamic User Cards

Imagine you’re building a user directory where you need to dynamically add user cards. Instead of creating elements manually, let’s use a <template>.

<template id="user-card-template">
  <div class="user-card">
    <h3 class="name"></h3>
    <p class="email"></p>
  </div>
</template>

<div id="user-list"></div>
Enter fullscreen mode Exit fullscreen mode

Now, let’s use JavaScript to populate it with data:

const users = [
  { name: "Alice", email: "alice@example.com" },
  { name: "Bob", email: "bob@example.com" },
];

const template = document.getElementById("user-card-template");
const userList = document.getElementById("user-list");

users.forEach(user => {
  const clone = template.content.cloneNode(true);
  clone.querySelector(".name").textContent = user.name;
  clone.querySelector(".email").textContent = user.email;
  userList.appendChild(clone);
});
Enter fullscreen mode Exit fullscreen mode

2. Rendering Modal Popups Dynamically

Ever needed a modal that appears only when triggered? A <template> can store the modal’s structure and create instances on demand.

<template id="modal-template">
  <div class="modal">
    <p>Are you sure?</p>
    <button class="close-btn">Close</button>
  </div>
</template>

<button id="show-modal">Show Modal</button>
<div id="modal-container"></div>
Enter fullscreen mode Exit fullscreen mode
document.getElementById("show-modal").addEventListener("click", () => {
  const template = document.getElementById("modal-template");
  const modalContainer = document.getElementById("modal-container");

  const modal = template.content.cloneNode(true);
  modal.querySelector(".close-btn").addEventListener("click", () => {
    modalContainer.innerHTML = "";
  });

  modalContainer.appendChild(modal);
});
Enter fullscreen mode Exit fullscreen mode

3. Creating Reusable Tables

If you're working with tables, <template> can simplify dynamically adding rows.

<template id="row-template">
  <tr>
    <td class="name"></td>
    <td class="age"></td>
  </tr>
</template>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
    </tr>
  </thead>
  <tbody id="table-body"></tbody>
</table>
Enter fullscreen mode Exit fullscreen mode
const data = [
  { name: "Charlie", age: 25 },
  { name: "Dana", age: 30 },
];

const template = document.getElementById("row-template");
const tableBody = document.getElementById("table-body");

data.forEach(person => {
  const row = template.content.cloneNode(true);
  row.querySelector(".name").textContent = person.name;
  row.querySelector(".age").textContent = person.age;
  tableBody.appendChild(row);
});
Enter fullscreen mode Exit fullscreen mode

Conclusion

The <template> element is a game-changer when it comes to creating reusable and efficient dynamic content. It keeps your HTML clean, boosts performance, and simplifies JavaScript DOM manipulations.

Next time you find yourself generating UI elements dynamically, give <template> a shot. Your future self will thank you!

If you are interested in exploring such technologies, and improving your productivity, Then LiveAPI will be a good option!

As a developer you will be dealing with API documentations to understand the codebase better. In some cases the repositories wont have an API Doc, which makes things harder.

To solve this issue, LiveAPI simply connects to your repository and instantly generates Interactive API documentation for you to use!

So if you are someone who would like to move more faster in your coding journey, feel free to give it a try!

Top comments (3)

Collapse
 
softwaredev_sk profile image
S K

I've used templates a lot in my projects (mainly extensions), but I feel the element is a much better fit for modals.
Isn't it?

Collapse
 
pxlmastrxd profile image
Caleb (pxlmastr)

Might have to try this when building my portfolio. Thanks for the post!

Collapse
 
jwp profile image
John Peters

Nice

Some comments may only be visible to logged-in visitors. Sign in to view all comments.