4 ms·
Templating in JavaScript, from Zero Dependencies on Up (2021)
- PaulHoule 2y agoIsn't there a <template> and <slot> mechanism in HTML you can use now?
- Klaster_1 2y agoLast time I checked, you still have to manually clone templates, query slots to fill and imperatively manipulate DOM. Did this recently and found the process very annoying compared to frontend framework template experience. Compared to banging out template literal strings, sounds like too much hassle, especially in Node.js, where there's no DOM by default, so you'd have to import a DOM library.
- wetpaws 2y ago[dead]
- funksta 2y agoThe 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
- toastercat 2y agoNice 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 https://jsr.io/@std/html/doc/~/escape Similarly, for XSS, the `xss` package on npm works great for Deno.
- nickreese 2y agoUsing 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.
- nickreese 2y agoFor those looking for something more robust... this is where I'd look: https://github.com/kitajs/html https://github.com/kitajs/html. Also Honojs (hono.dev) has a built in HTML function which is quite good and I've used extensively.
- pandemic_region 2y agokitajs already being a fork of https://github.com/nicojs/typed-html https://github.com/nicojs/typed-html . What makes this one more robust or stable then?
- riiii 2y ago> I want JS projects that don't break 6 months after the last build I was somewhat recently trying to get a 3 year old JS project working. I felt like I was alone in an Egyptian pyramid wondering if I had missed the funeral.
- mbivert 2y ago> 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. I've settled on ``document.createElement()``: it's a bit rough visually, but that's pretty much the only downside (I've found the time gained with templates to be negligible in front of the overall development time). A big bonus of dealing with genuine objects is that there's room to extend them to e.g. store additional state/data/handlers/etc., which might otherwise be cumbersome to be set via attributes & cie.
- TeMPOraL 2y agoAn even bigger and underappreciated bonus of dealing with objects is that you're working at the correct abstraction layer. Using templates that glue string together is doing the Wrong Thing, and this is how you get XSS (or outside of HTML, injection class vulnerabilities specific to what you're writing, e.g. SQL injection when gluing strings into queries).
- diegof79 2y agoIt 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.
- niedzielski 2y agogood article. the point would have been more salient if the jimniels/html micro dependency had been inlined.
- nosefurhairdo 2y agoReact-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.