July 14th, 2024

Templating in JavaScript, from Zero Dependencies on Up (2021)

The article explores dependency-free templating in JavaScript, favoring template literals and functions over traditional methods like JSX. It demonstrates creating components with functions and suggests a small dependency for enhanced syntax. Benefits include simplicity and flexibility for rendering in diverse environments.

Read original articleLink Icon
Templating in JavaScript, from Zero Dependencies on Up (2021)

The article discusses templating in JavaScript without dependencies, focusing on using template literals and functions to generate HTML. It contrasts this approach with traditional templating languages like JSX, highlighting the simplicity and flexibility of the dependency-free method. The post explains how components can be created using functions that return strings, showcasing examples and patterns for composing components with children. It also mentions enhancing the syntax with a small dependency for a more JSX-like experience. The article concludes by emphasizing the benefits of zero dependency templating, enabling rendering in various environments without additional tools or complexities.

Related

Link Icon 8 comments
By @nickreese - 5 months
Using this pattern has been a gift. It is really freeing to write templates without a framework and worrying about SSR.

Years back I dove completely in on Svelte and even wrote my own SSG that helped kick off the Islands/partial hydration crazy... Elder.js. It was fun, but burnt me out of open source.

Today, I only pull for a template language and deal with SSR/Hydration for pages that really need it.

I want JS projects that don't break 6 months after the last build. The way you get that is by not adding more dependencies and not chasing the latest craze.

By @funksta - 5 months
The fact that XSS isn't covered (or even mentioned!) in an article about building your own HTML templating system is a major, and dangerous, oversight
By @keb_ - 5 months
Nice article; I do this often with small server-side rendered Deno projects. I haven't felt the need to upgrade to a "proper" templating library yet.

I noticed the article doesn't mention escaping HTML, which you probably should do if you're expecting user input. Fortunately, such a utility is included in Deno's standard library: https://jsr.io/@std/html/doc/~/escape

Similarly, for XSS, the `xss` package on npm works great for Deno.

By @diegof79 - 5 months
It all depends on the requirements and quality attributes.

For example, a plain string template will not help with selective DOM updates; that’s why we end with a tree DSL like JSX.

For many different reasons, you’ll like to have lazy component rendering. That’s why using `Component(props)` is not the same as `factory(Component, props)`.

I’m not saying that the author is wrong or right. If you only want a server-side template or one-off template for a simple page, then using JS/TS string templates is better than relying on a DSL like Handlebars or Pug. The client-side story is a bit different, and that’s why there are so many frameworks for it.

By @nosefurhairdo - 5 months
React-dom/server APIs are great for this. Writing jsx (as opposed to template literals as in OP) is generally a nicer DX, and you can keep the setup fairly minimal.
By @PaulHoule - 5 months
Isn't there a <template> and <slot> mechanism in HTML you can use now?
By @niedzielski - 5 months
good article. the point would have been more salient if the jimniels/html micro dependency had been inlined.