HTML Basics

HTML Introduction HTML Basic HTML Comments HTML Tags/Elements HTML Attributes HTML Formatting HTML Block and Inline HTML Charsets HTML Classes HTML Colors HTML Div HTML Headings HTML Id HTML Iframes HTML Images HTML File Paths HTML Tables HTML Layout HTML Lists HTML Links <a> HTML Paragraphs HTML Quotations HTML JavaScript HTML Emojis HTML URL Encode HTML Entities HTML Computercode HTML Symbols HTML Styles

HTML Forms

HTML Forms HTML Form Elements HTML Form Attributes HTML Input Attributes HTML Input Regex HTML Input Form Attributes HTML Input Types

HTML SEO

HTML Head HTML Page Title HTML Responsive HTML Semantics HTML Favicon

HTML Graphics

HTML Canvas HTML SVG

HTML Media

HTML Media HTML Audio HTML Video

HTML Reference

a abbr acronym address applet area article aside audio b base basefont bdi bdo big blockquote body br button canvas caption center cite code col colgroup data datalist dd del details dfn dialog dir div dl DOCTYPE dt em embed fieldset figcaption figure font footer form frame frameset h1_-_h6 head header hgroup hr html i iframe img input ins kbd label legend li link main map mark menu meta meter nav noframes noscript object ol optgroup option output p param picture pre progress q rp rt ruby s samp script search section select small source span strike strong style sub summary sup svg table tbody td template textarea tfoot th thead time title tr track tt u ul var video wbr

How to Use the HTML <template> Tag

The <template> tag in HTML is used to define a content template that can be used to render content on a webpage. The content inside a <template> tag is not rendered when the page is loaded, but can be cloned and inserted into the document using JavaScript. This allows for a more efficient way to manage and render content on a webpage.

Example of Using the <template> Tag:


<template id="userTemplate">
  <div>
    <h2>{{name}}</h2>
    <p>{{email}}</p>
  </div>
</template>

<script>
  // Get the template element
  var template = document.getElementById("userTemplate");

  // Create a new user object
  var user = {
    name: "John Doe",
    email: "johndoe@example.com"
  };

  // Clone the template content
  var clone = document.importNode(template.content, true);

  // Update the template content with user data
  clone.querySelector("h2").textContent = user.name;
  clone.querySelector("p").textContent = user.email;

  // Append the cloned content to the document
  document.body.appendChild(clone);
</script>

In the example above, we have a <template> tag with an id of "userTemplate" that contains a simple user profile. We use JavaScript to clone the template content, update the user data, and append the cloned content to the document.

Main Attributes of the <template> Tag:

Attribute Description
id Specifies a unique id for the template
content Contains the content of the template

Overall, the <template> tag is a powerful tool for creating reusable content templates in HTML and dynamically rendering them on a webpage using JavaScript.