Maintaining old template code is frustrating. The longer and larger conditional clauses and inherited blocks become, the less readable the code. It is hard to understand why at this point in the development of web applications we are still generating our very tree-like HTML as if it were a flat file. Given how slowly browsers change, it is not likely we will be moving to another format any time soon. Friction occurs between the declarative structure of HTML and the procedural programming language meant to generate it (e.g. Django, Cheetah). The template accumulates cruft faster than other components because it is always a poor attempt at both procedural and declarative code.
Looking at HTML as a tree, one solution would be to write HTML using a language that is naturally tree-structured. Functional languages fit this mold. This will only be a maintenance boon if application logic is in a language such as Erlang or Haskell.
Since most application logic is written in a procedural language (Python, Ruby, Java), why not handle the creation of HTML in this way? We already have a model in the browser for HTML manipulation idiom for procedural programming: jQuery. jQuery's growing popularity is due to the simplicity with which one can describe DOM manipulations. Bringing this model to the server side would improve clarity of front-end code in the same manner that it has done on the client side.
Consider a block of HTML describing a rudimentary document:
<html>
<head>
<title>A Document</title>
</head>
<body>
<div id="content">
<ol class="things">
<li>Example Thing</li>
</ol>
</div>
</body>
</html>
Think of this block as the prototype of the thing we want to describe by means of a procedure. Instead of aiming to build exactly and only the output we desire, the proper output will be achieved by starting at a clear example and proceeding through a series of modifications. This strategy is borrowed from the prototypal inheritance found in languages like Javascript.
The following code is an example of a potential API for Python. It should look very familiar to anyone who has written jQuery.
html = Q(html_doc) # load the above DOM into a jQuery-like object
html('title').text = 'A More Interesting Title'
for i, item in enumerate(html('#content ol li').remove().duplicate(10)):
item.text = str(i) + ' item!'
html('#content ol').append(item)
html.render()
Template-based HTML generation is by contrast the class-based inheritance model of the frontend world. It has all the same complexity problems. Over time, templates that take advantage of inheritance (such as blocks in Django or Cheetah) will require a reader to look at several files at once to reconstruct the execution of the template. Cleverness is obfuscation. Deep inheritance trees are inarguably clever.
One may object that providing no way for non-programmers to create HTML would cause friction between designers and programmers. Under the prototypal model, a designer's mockups are the prototypes that are inputs to the procedure. Non-programmers frequently write poor code, which contributes to the maintenance burden. Recognizing that building the HTML output for any application requires a programmer, one ought to provide them immediately with the most powerful tools for clearly expressing HTML. There is no better one than the language and style in which they are building the application.
Does this sound interesting? I started a project to implement it for Python using the lxml libxml2-backed library. Check out pyQuery on github. Contributors are welcome.
