Genshi: A Real Web Templating Language

I believe that it is very important for web sites to comply with HTML/XHTML standards. The HTML specification is very detailed in how it should be represented and thus browsers that render HTML try to follow the specification as close as possible. Most of the template languages that I have used(Django, Mako) are text-based. This means that it the template processors don't know anything about HTML. Everything that it works with is just text(as far is it knows).

Unfortunately, we know that we aren't dealing with just text. In fact, we need to make sure that our templates contain valid HTML and when generated are still valid. This is fine, but it definitely is a pain and adds some unnecessary work. Up until I used Genshi, my attitude was that it wasn't a big deal and I would just validate it all myself when coding. I soon learned though that as your templates become more complex and detailed it becomes much more difficult to keep validating everything manually. This is where Genshi comes in.

Genshi is an XML-based template language. That means that it uses an XML parser to work with your templates and then finally generate a valid XML document. For those of you that don't know, HTML is very similar to XML syntax. It is not identical, but for our purposes its close enough that an XML template parser works with HTML documents fine.

So what does this mean to a developer? Genshi will make sure that all of your templates have correct XML syntax and the generated content is also valid. If you forget a closing tag in your template, don't quote an attribute properly, or many other common syntax errors, Genshi will tell you. It makes your life much easier, or at least it made mine easier.

If you are generating content for the web, presumably in XHTML/HTML, then to me it only makes sense to use a template language that is made to work with that content.

Written on October 17, 2007