DEV Community

Cover image for The <template> Tag: A core HTML Feature That Simplified My JS

The <template> Tag: A core HTML Feature That Simplified My JS

Richa Parekh on October 13, 2025

I’m currently developing a conversational AI web application. I’ve been building it completely from scratch, following a structured SDLC approach. ...
Collapse
 
dannyengelman profile image
Danny Engelman

⛔ The Problem: Too Much HTML in JavaScript

I'd say the problem is using the wrong HTML

Every <div> can be replaced with a Custom Element

dashed-html.github.io

Collapse
 
richa-parekh profile image
Richa Parekh

Thanks for sharing. I will check it out

Collapse
 
aadswebdesign profile image
Aad Pouw

Sure, the issue you brought in and how you solved it, is great and I learned from it.

But when I read 'JavaScript file became messy ', then I guess you use a single js file?
Just as an advice.
With a module approach and a proper file structure your js flles hardly becoming messy at all and are better maintainable too.

Collapse
 
richa-parekh profile image
Richa Parekh

Thanks for the advice! I’m currently in the early stage of development, focusing on frontend interactivity, so I’m using a single JS file for now. I do plan to refactor it into modules soon for better structure and maintainability.

Collapse
 
barney-parker profile image
Barney Parker

It's funny, there is so much hate for PHP because of the mixed code/markup, but sure, do it in JS and suddenly it's a good thing 🤣

Collapse
 
aadswebdesign profile image
Aad Pouw

A little late but for sure I do like PHP very much and I used to do a lot with it.
At present not at all but that's for another reason.
Then, Yes the reason that I like to use JS in a modular way has all to do with my PHP experiences because with that, having a proper file structure is a standard.

Collapse
 
m-a-h-b-u-b profile image
Md Mahbubur Rahman

Fantastic article, Richa!! I love how you showcased the power of the tag — it’s such an underrated gem in HTML. Your example perfectly demonstrates how it streamlines JavaScript, keeps code cleaner, and boosts scalability. It’s refreshing to see elegant, native solutions that reduce complexity without extra libraries. Definitely inspired to refactor some code now!

Collapse
 
richa-parekh profile image
Richa Parekh

Glad you find this useful. And yeah, it's amazing how simple native features can make such a big difference.

Collapse
 
lucanos profile image
Luke Stevenson

If you think this is good, you should check out JSRender. Basically offers the ability to separate HTML from JS creating reusable templates, but also lets you push a JS array or object into it and handles all of the content placement.

jsviews.com/#jsr-quickstart

Collapse
 
richa-parekh profile image
Richa Parekh

Thanks for sharing. I will look into that.

Collapse
 
smahies profile image
Maheeee

When a person code in spring boot for almost 2 years no front end touched, then even basic html tag looks too much scary :(

Collapse
 
richa-parekh profile image
Richa Parekh

Haha, switching back to the frontend after backend work feels like entering a different world. 😄

Collapse
 
cristiansbardelotto profile image
Cristian Sbardelotto

that's huge! in 3 years using HTML, never heard of this tag one single time. i wonder how long it was hidden from me 😂

Collapse
 
richa-parekh profile image
Richa Parekh

Haha, same here! I hadn’t come across this tag until recently. When I needed to create a dynamic UI and my HTML started getting too lengthy to manage in JS, I looked for alternative approaches online, and I’m glad I discovered this underrated HTML feature!

Collapse
 
mirko_ruhl_338358040bba2b profile image
Mirko Ruhl

If you are amazed by template, React will blow your mind

Collapse
 
richa-parekh profile image
Richa Parekh

Yeah. React indeed is best. But it is about how a built-in feature can have the capability to solve issues or implement features without any library or framework.

Collapse
 
barney-parker profile image
Barney Parker

If you are amazed by React, Custom Elements will blow your mind!

Collapse
 
istiuak_ahmed_244fcdf83c3 profile image
istiuak ahmed

In vue js the template tag is heavily used .but does it does not work in the same way.
So if I want to use template tag in vue how do I do it.
This might cause conflicts. ( although this template tag is really not needed in frontend framework. Just curious 🤔)

Collapse
 
richa-parekh profile image
Richa Parekh

I’m not very familiar with Vue.js, but you’re right, tag works differently there and might conflict with the native one.

Collapse
 
juansecu profile image
Juansecu

Great post, Richa! This is truly useful when optimizing web applications!

Collapse
 
richa-parekh profile image
Richa Parekh

Glad you find this useful!

Collapse
 
tarzan9192 profile image
Joshua Whitney

I hate writing JS (in any form), but found myself having to do so for work lately. This was actually a pretty useful post. Thanks!

Collapse
 
richa-parekh profile image
Richa Parekh

Thanks for appreciating! Glad you find this useful.

Collapse
 
iain_tarrant_704ddbb77152 profile image
Iain Tarrant

Or you could just use datastar or htmx and merge the new html fragments directly into the dom from the backend. No js required.

Collapse
 
barney-parker profile image
Barney Parker

Why use a library when the spec already has the capabilities built in?

Collapse
 
sanjay_kutakanakeri_31247 profile image
Sanjay Kutakanakeri

Such a an incredible feature and this happen to come to me on perfect time, now i can keep my html in my html and use js api to set attributes and inner text. Thankyou, nice explaination.

Collapse
 
richa-parekh profile image
Richa Parekh

Thanks for appreciating! Glad you find this useful.

Collapse
 
automatic_9a1a5b02ba0d308 profile image
Automatic

hmmm, I have no choice than to watch senior developers " pop my bubble" as they interact using different kind of concepts I have not even discovered, hmm
Good post thanks 😞.

Collapse
 
richa-parekh profile image
Richa Parekh

Ha ha😄. Glad you like this!

Collapse
 
evisar profile image
Visar • Edited

Imagine being able to use this tag to create reusable Web Components. Or fast forward 10 years and imagine not using this tag and use the tagged template literals html``.

Collapse
 
paul_levitt_2eb5cf3fbe227 profile image
Paul Levitt

That’s exactly why it and the slot element have been added - check out developer.mozilla.org/en-US/docs/W...

You could use tagged template literals, but then you’re reintroducing the coupling of the html and js - limiting flexibility. And impacting performance - as mentioned the template tag’s DOM node is parsed and ready to go, even when cloned; the tagged temple literal would require parsing each time you append it.

Collapse
 
evisar profile image
Visar

There's nothing wrong with using js to build html.
What is wrong is if that jshtml function does more than return html. Then it's wrong.

Otherwise it's a matter of taste:
Arr.map(item=> htm'

${item}</>')

${item}

Thread Thread
 
paul_levitt_2eb5cf3fbe227 profile image
Paul Levitt • Edited

Never said there was anything “wrong” with using js to generate HTML - just highlighting the template elements plus points made by Richa in the “Why is this important?” section and conclusion.

However; Using any form of string literal HTML to manipulate the DOM goes against the point of both (yes we can do it, but should we) - HTML defines the initial structure and content of a webpage the DOM, initially generated from the HTML, represents the structure and content of the page after that, and provides us an API to programmatically interact with and manipulate it.

I know it’s super simple to generate/inject HTML strings in manipulating the DOM structure (I’ve done it too) but it’s not the intended approach, not performant, and easily replaceable with native APIs

Rant over - cheers for listening 🤣

Thread Thread
 
barney-parker profile image
Barney Parker

This is the way!

Collapse
 
barney-parker profile image
Barney Parker

Really nice article describing the standards-compliant way of templating without the need for an external library!
I really liked the simplicity and I 100% agree, it keeps code better organised. Thanks for taking the time to write this up.

Collapse
 
jose_elias_d6027a60832a2e profile image
Jose Elias

Using HTMX would have been way simpler and easier, and with pretty much zero javascript.

Collapse
 
barney-parker profile image
Barney Parker

Zero js.... Except for the library, and the non-standard attributes, and probably some js anyway.... 😆