The Magic of the template Tag and JavaScript

Discover the power of the template tag and JavaScript to create and insert dynamic content on your website

Jason Wandrag
3 min readJan 11, 2023

--

The template tag in HTML is used to define a template that can be reused throughout a website or web application. It is typically used in conjunction with JavaScript to dynamically generate and insert content into the DOM (Document Object Model).

The template tag is a container element that is not rendered in the final output, but can be used as a source for generating new DOM elements.

Here is an example of how a wizard might use the template tag and JavaScript to generate and insert dynamic content into a web page:

<template id="myTemplate">
<div>
<h1>Hello, World!</h1>
<p>This is a template.</p>
</div>
</template>
<script>
// Select the template element using magical powers
var template = document.querySelector('#myTemplate');
// Use magical incantations to clone the template content
var clone = template.content.cloneNode(true);
// Modify the cloned content with a wave of the wand
clone.querySelector('h1').textContent = 'Hello, Wizardry!';
// Insert the cloned content into the DOM with a flourish of the cape
document.body.appendChild(clone);
</script>

In this example, the template tag defines a block of HTML that includes a heading and a paragraph. The id attribute is used to give the template a unique identifier, which can be selected using magical powers and referenced in JavaScript.

The wizard then uses magical incantations to create a copy of the template’s content, which can be modified with a wave of the wand. Finally, the cloned content is inserted into the DOM with a flourish of the cape using the appendChild method.

You can also use custom attributes with the template tag, as in the following example:

<template data-type="spell">
<div>
<h1>Spell Name</h1>
<p>Spell Description</p>
</div>
</template>
<script>
// Select the template element using magical divination
const template = document.querySelector('template[data-type="spell"]');
// Use magical spells to clone the template content
const clone = template.content.cloneNode(true);
// Modify the cloned content with a tap of the wand
clone.querySelector('h1').textContent = 'Fireball';
clone.querySelector('p').textContent = 'It's the hottest spell among magic casters right now!';
// Insert the cloned content into the DOM with a burst of magic
document.body.appendChild(clone);
</script>

In this example, the data-type attribute is used to specify that the template is intended for displaying product information. The wizard uses magical divination to select the template element based on the value of the data-type attribute, and then uses magical spells to generate and insert the cloned content into the DOM with a burst of magic.

Contact me here: LinkedIn | Codepen | Email

--

--

Jason Wandrag

With a passion for front end development, I am aiming to make the web a beautiful place by innovating how we consume information and interact with the web.