Hacker News new | ask | show | jobs
by webmaven 1852 days ago
> And kids, that's the story of how I met your templating system.

> ALL of them suck. ALL of them are incredibly hard to lint, test, get right. They scale badly. They're all awful.

That's not really true. But fairly incredibly, it seems relatively little attention has been given to the design of HTML templating languages (certainly there are a lot of them, but most seem to have accreted rather than been designed).

Anyway, the least weird templating language that was designed that I am aware of is the TAL[0] syntax, which has as it's main constraint that unrendered templates must still be valid HTML. The original motivation for this was to eliminate the terrible workflow problems of round-tripping templates between a web designer and a (backend) developer, but there turned out to be other benefits, like taking care of linting difficulties. It is also rather deliberately not Turing-complete, as that has proved an 'attractive nuisance' that causes more problems than it solves.

The TAL syntax is most strongly associated with Python frameworks (I would guess that Chameleon[1] is the most popular implementation), but there are implementations for most prominent languages[2].

> You know what's a robust API for building HTML that you already know, that is already supported by virtually everything? The DOM.

How popular is SSR in JS using the DOM directly, rather than a template language of some sort (eg. Handlebars, Jade, Nunjucks)?

[0] https://en.m.wikipedia.org/wiki/Template_Attribute_Language

[1] https://chameleon.readthedocs.io/en/latest/index.html

[2] implementations (sometimes several) exist for JS, Java, C#, Perl, Raku, PHP, Python, XSL, Go, and Common Lisp.

2 comments

Thymeleaf templates are also valid HTML when not rendered. Of course you still have to put some thought into them to make them believable, by e.g. providing placeholders for elements that would be rendered in cycles.

Yeah, I find Thymeleaf to be nice, especially when coupled with Spring. I'm always happy when we decide to use it instead of bloating everything up with a separate Angular application.

I'm not a Java developer, so I wasn't aware of Thymeleaf.

How does it compare to the Java implementations of TAL?

TAL looks nasty. If you want conditionals in your HTML, just use conditionals. Don't hamfist it into the HTML syntax.

Twig or Pug/Jade are the best ones I've seen. C#'s Razor seemed pretty good too in the brief time I used it.

If you have some other way of being able to view an unrendered template in a browser and have it display sanely, I'd be very interested to know about it.