The problem with templating systems


In the past I've discussed my use of the PyMeld templating system. Meld is rather neat in that it converts an html string into an object model which can then be manipulated like one might manipulate any tree in Python. This has a number of upshots, mainly related to the seperation of code and presentation. In this case I have only 100% valid XHTML that isn't contaminated at all with a templating language that is either plaintext/comments or attributes embedded into the XML. Meld on the otherhand uses the id attribute to build a tree, but the tree is not strict, in that you can access an any-depth tree member from the top of the tree by id. This has some problems in that a) everything has to have id attributes, and b) id name conflicts can become a serious problem.

Meld's implemented by basically using a regular expression to find matching IDs in an HTML string and then inserting/overriding those portions of the string. For a large template this is a rather expensive operation. Even for small templates it has issues. I finally hit a situation where I had to give up on Meld.

So I was working on this site to do TV listings, and like usual I used Meld to generate my templates. At first the code was doing the RSS parsing and fetching on every run, and it was a bit slow, so I figured when I moved the RSS data into a database the site would get a lot faster. I was quite wrong. Meld was taking 3-5 seconds per episode to generate the entries! With only 15 entries on the page that was clearly not going to work, so I went to find another solution.

In the past I'd started to write a system that mimiced the DOM api but was a bit easier to work with. In about an hour I tossed together an HTMLParser setup that parsed the HTML and Text elements into a tree that could then be searched through using some DOM like functions. Right now it's a bit harder to work with since I have to create DataElement and HTMLElements when I want to insert things instead of just inserting text directly like Meld allowed. I'm hoping to make this process a bit easier. I'll also be merging the base class with the HTMLElement class since they do more or less the same thing.

I'm tenativly calling it Kiln and it's able to template by TV listings site fast enough that I don't notice it as being a problem. I do hope to sit down and make it really fast in the future though, and I'd really like to see this more manipulative type of templating become popular. I think that moving the code out of the template is a really good thing, since it seems one of the intiial reasons to use templates was to move the HTML out of code, and yet in response we just seem to have done the opposite!


published at Mon Apr 9 19:29:08 2007 (-0700) by alexbl
Tags: www, python, 2.0, development, meld
| |