Check out a free preview of the full Vanilla JS: You Might Not Need a Framework course

The "HTML Templates" Lesson is part of the full, Vanilla JS: You Might Not Need a Framework course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano discusses creating markup containing HTML content that can be reused, rendered, and dynamically modified. CSS style declarations made in templates will apply to the entire document by default.


Transcript from the "HTML Templates" Lesson

>> The other important part is the template element. The template element is a fragment of markup that can be cloned and inserted into the document at runtime with the reusable HTML content that can be rendered and modified dynamically. I'm not sure if you understood from there. But quickly, I'm not sure if you remember that we have the template elements here.

These are template elements. What's the template element, a tag that the browser ignores completely. It's in the DOM but it ignores what's inside. It says, I'm not gonna touch it, I'm not going to render it. It's a template for a JavaScript code that wants it. Before the template, we were using some hacks.

I'm not sure if we have seen these before, like script with a type wherever, with a language wherever. Not sure if you have seen this. So because the browser was saying hey I don't know that the scripting language the browser was ignoring that, okay? And then using the DOM API we were getting the content and then using JSON.parse, or Stringify, or wherever,okay.

Well, we don't need to use those hacks anymore, we use template. And then with the DOM API, we get the contents of that template, we clone them, we create clones of them, that's it. Template and we are creating instances of that template that we checked in a real DOM.

Okay makes sense. So it's a way that we have to define the HTML of a web component in the HTML file. Okay so similar to what Angular things what a template is okay the thing here is that it goes in the DOM. So now I'm going back to one of the questions that we had earlier about the fragment document.

So a document fragment in the DOM, it's something similar to what template is, is to create a DOM in memory that is not a real DOM. It's like a virtual DOM, so you can play with that, but it's not actually affecting the real DOM. And in that sort of things is similar to what template is doing.

So the template is pretty simple. We have already seen that. Typically, we set an ID it's not mandatory, but because we will need to access it later to clone it, we get it. And to clone it, typically, we do that in the connected callback method of the custom element.

We get the template with getElementById or quelySelectors or somehow, we call content.cloneNode and then we append the content the clone not the template. Because the template is like read-only and parsed. Okay, so we clone it and then we use it, that's how it works.

By default, the nodes of our custom elements are part of the same page DOM. So for example, CSS style declarations applies to all the documents. What? Let me show you something. Let's say that we grade the template, and the template has an e style declaration setting all the H1 as reds, because it has an H1.

But by default, this style declaration goes to the whole document, so outside of the template, and it's going to change all the H1s in the page, not just the H1 of this template,

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now