Hacker News new | ask | show | jobs
by klibertp 4166 days ago
There's one technology, available everywhere including most toasters, which allows you to do this (cache HTML template and just substitute the data) and it can work on both back- and frontend with no problems, is crazy fast and even leaves you with something readable when it breaks.

It's called XML with XSLT.

Somehow it isn't very popular. But hey, we got this idea of "rendering pages on the backend or the frontend (or both)" from the same code lately!

1 comments

> It's called XML with XSLT.

But that can only generate XML/XHTML, right?

Also, fast compared to what? Many options are fast if all you do is replacing some values. Static pages can just be written out from memory or disk, a whole nother level of fast :)

No, XSLT is perfectly capable of generating plain text, which means it can also generate JSON, YAML or what have you. It's true that generating malformed XHTML would be rather hard, though.

As for fast: compared to any other templating engine for server side languages. It was very long ago when I tested this, but for rendering a simple blog page XSLT with lxml (so completely on the C side) was much faster than Jinja2. I suspect only things like Mustache would have a chance here, but then again: XSLT is Turing-complete (probably accidentally, but still).

And that's before taking into account that you don't have to do rendering server-side at all - you can just serve static files and rely on the browser to render them. I don't know if it would be faster than serving static files and rendering with JS, but it has one advantage: it works even for people who have JS disabled.

There are many problems with XSLT, though. XSLT 1.0 is almost unworkable and XSLT 2.0, while specified, is nonexistent: nobody bothered to implement it. Once you start using implementation specific ways of extending it, you lose the benefit of being able to render on a client. Turing completeness can be a trap, too: you can code infinite loop in XSLT, and good luck debugging it (or even recognizing where do you loop in the first place...).

Still, XML and XSLT are a very nice technology, which should be used much more often. The problem, I suspect, is that XML is "yuck" nowadays, which means no one wants to use it, learn it or improve it.