< template > vs < div >
Tags: javascript, vue, webdev, html
Published: 2021-08-15 20:08:14

You'd relate to this if you've been working with likes of Vue and React. The &lt;template&gt; tag also finds use in web components development. You may have considered this trivial enough to let it go without trying to find out '**why use &lt;template&gt; and why not &lt;div&gt; or even &lt;span&gt; which we tend to throw in wherever**'. If you fit in that unsubstantial description, probably you'll get to learn something.

![Intro image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zvl1tyensffxzdbti5n4.jpg)

<hr>

Before we start I'd throw in a few points to remember

1- &lt;template&gt; is **inert**
2- &lt;template&gt; has only one [property](https://developer.mozilla.org/en-US/docs/Web/API/HTMLTemplateElement#properties) of its own i.e. 'content'
3- &lt;template&gt; accepts **global attrbiutes** ('class', 'id', etc..) available on almost anything in HTML.
4- A template's **content** is not a valid target for events

When I say &lt;template&gt; is inert. It simply means that if you put a template tag in your HTML code, it will **sit there doing nothing** by itself. The template tag along with its content/children won't be rendered as a part of the DOM, not even on the next [reflow](https://developer.mozilla.org/en-US/docs/Glossary/Reflow). Obvious question, how is that useful? To answer whys and hows, lets first get over the whats. So, the template tag won't reveal itself in the DOM tree unless you explicitly handle it in your JavaScript and make it do so. Its important to note that the **DOMParser** parses everything including the temlpate tag but in this case only to check **syntax validity**. Alright, to get the markup inside &lt;template&gt;&lt;/template&gt; you need to

1- Get the content from template. The content is an instance of **DocumentFragment**
2- Since DocumentFragment is an implementation of the **Node interface**, you can use [cloneNode()](https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode) to get a copy of the template's content to then append it somewhere in the DOM. Alternatively, you can select a part of the content using [properties available on the DocumentFragment](https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment#:~:text=new%C2%A0DocumentFragment%20object.-,Properties,-This%20interface%20has) interface or even push the entire DocumentFragment into the DOM tree. ***If you choose not to clone the content***, you'll notice that the part of the document fragment once appended to some node in the DOM tree, is not to be found in the fragment anymore.

```javascript
---------------------------- HTML ----------------------------
  
<ul id="container">
  <b>Basket</b>
</ul>

<template id="inert">
  <li>Apples</li>
  <li>Oranges</li>
  <li>Milk</li>
</template>

------------------------- JavaScript -------------------------

const template = document.getElementById("inert");
const documentFragment = template.content;

console.log(documentFragment.childElementCount); // 3

// const deepClone = true;
// const nodeToAppend = documentFragment.cloneNode(deepClone);
const nodeToAppend = documentFragment.firstElementChild;
const container = document.getElementById("container");
container.appendChild(nodeToAppend);

console.log(documentFragment.childElementCount); // 2
```
<center><h6>[Try on Codepen](https://codepen.io/mayankav/pen/VwbNxjQ)<h6></center><br>


That said, what does it mean when I say a template's content is not a valid target for events? We now know that content of a template is an instance of DocumentFragment which inturn can be considered a light weight version of [Document](https://developer.mozilla.org/en-US/docs/Web/API/Document). DocumentFragment doesn't make it into the real DOM tree. If you know this you won't mistake it for a target of any sort of event or you may still end up doing something like so

```javascript
const template = document.getElementById("inert");
const fragment = template.content;

const nodeToAppend = fragment.cloneNode(true); // just another instance of DocumentFragment
nodeToAppend.addEventListener("click", () => {...}) // Mistake
```
<center><h6>[Easy to make mistake](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template#avoiding_documentfragment_pitfall)<h6></center>

<hr>

That alone clarifies how &lt;template&gt; differs from &lt;div&gt;, &lt;span&gt; or any other tag for that matter. Template does exactly what its name suggests, its meant to be used as a temporary case for your unfinished, yet to be processed markup. You may argue that you can use the hidden property on &lt;div&gt; and use it like a &lt;template&gt; but then your unfinished markup will still be a part of the DOM causing **side effects** (n/w requests for images, scripts etc..) which you may not want. The template element and its contents are inert i.e. not rendered in the DOM, until you grab a reference to it with JavaScript and then append its content to some node in the DOM. You can use a &lt;template&gt; tag when you want to load something into the DOM after processing it in JavaScript, [which is what Vue does](https://vuejs.org/v2/guide/syntax.html#:~:text=and%20HTML%20parsers.-,Under%20the%20hood,-%2C%20Vue%20compiles%20the) (processes the elements and makes them reactive). Try replacing &lt;template&gt; with &lt;div&gt; in one of your SFCs and see how Vue reacts with ***[Vue warn]: Failed to mount component: template or render function not defined***.

You could have done well without reading this but I feel it never hurts to know a little more of everything you come across at work. This shall defnitely add value in some way or the other. If you like paying attention to details, I would recommend an old post I wrote on [key to responsive websites](https://dev.to/mayankav/the-key-element-to-responsive-websites-31e5). You may find it useful. If you want to read more on html templates, [go ahead](https://www.html5rocks.com/en/tutorials/webcomponents/template/).

<hr>

##Originally Posted Here -
https://mayankav.webflow.io/blog/template-vs-div