News for hackers. Stuff that matters.
Re:Templating in HTML by apatheticonion on Tuesday October 04, 2022 @07:01PM (#33088333)
I think it's mostly performance as the browser can add performance optimisations for HTML but not unevaluated JavaScript.

The alternative is a "display: none" block, but that triggers a calculation of styles, where <template> can never be rendered so it's evaluated (likely in parallel to JS) by the browser without style calculations.

The optimisations lose weight if the <template> tag is added programatically later. For it to be maximally efficient; all of the <template> tags must be inlined in your HTML prior to your application loading. You can play around with loading it asynchronously to ensure nothing blocks.

Re:Templating in HTML by spankalee on Tuesday October 04, 2022 @09:55PM (#33090087)
Adding a <template> tag later is great for perf too because the contents are still inert and fast to clone.