Why templating systems are the wrong answer for simple HTML generation

January 15, 2009

One common proposed solution to the problem of simple HTML generation is a templating system. Apart from any practical issues (all of which can be worked around), I've come to realize that I believe that they're the wrong answer in general, because templating systems are solving a different problem.

At least for me, the problem simple HTML generation solves is that HTML is annoyingly verbose and picky to write directly by hand (and partly as a result is difficult to cleanly embed into Python code). This is the same problem that is solved for by using simple markup, and it's such a popular thing to do for the same reason that high level languages are popular: you want to have a compact and clear way of expressing your intentions, one that's quick to write and is not cluttered up with unimportant details.

The problem that templating systems solve is that if you try to generate complex pages in string-bashing code, you wind up with a horrible entangled mishmash of code and HTML that is effectively unreadable; you can't easily see either the shape of the HTML that will come out the end or what the code is doing. This is an important problem, but it is a different problem than not having to write HTML by hand; a solution to one is not necessarily a solution to the other.

(Some templating systems also try to solve the 'HTML is annoying to write' problem, but often they do not. In fact, one popular model for templating systems is HTML plus additional markup, which is exactly the reverse of what you want for simple HTML generation.)

Comments on this page:

From at 2009-01-15 08:45:15:

I don't get it. If the html you're trying to generate is verbose and annoying to write then you're not generating 'simple html'.

This: from html import * page = HTML(HEAD(TITLE("Qwerty")),

           BODY(H1("Qwerty's page"),
                P("Some text goes here.")))

print page

takes about as much time to write as the normal html version in a string with tokens and it's not all that much more readable.


By cks at 2009-01-23 11:43:26:

I put my view of what I'll call the HTML tax into a new entry, TheHTMLTax. In general, I think that people's perceptions of the annoyances of writing HTML by hand vary a lot, and I'm clearly fairly far on one end of the spectrum.

Written on 15 January 2009.
« Documenting the kernel.sem sysctl
A lament about modern NFS development »

Page tools: View Source, View Normal, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Thu Jan 15 02:40:35 2009
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.