DEV Community

Binyamin Green
Binyamin Green

Posted on • Originally published at binyamin.substack.com

1 1

All About the Template Tag

The template tag was first introduced in HTML 5.2, and has not received much fame since. It has ninety-five percent global browser support (source), so there’s not much of a reason to avoid it.

But what does the template tag actually do?

  • It stores HTML for later, so complex items are easy to replicate.
  • It’s invisible to users, screen-readers, and Search Engines, making SEO and accessibility (a11y) simple.
  • Since you are writing HTML in the HTML file, your code will read as simply as if it were written for two-year old children. (Disclaimer: I have not tested this).

Let’s give an example of somewhere we would use the template tag.

Consider a static page which pulls news from an API such as Hacker News’. We aren’t using React, nor Express. We can pull data from the Hacker News API, but how do we display them to the user?

  1. We could use document.createElement. However, someone reading our code wouldn’t look for HTML inside the JavaScript file.
  2. We could set the innerHTML to an HTML string. Except, this exposes a security risk called Cross Site Scripting. Thank you to moomin for pointing this out.
  3. The best option seems to be the template tag.

You can learn more about using the template tag on MDN. Also, check out the Hacker News example on Codepen.


Note: This post first appeared on my blog.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay