Genshi: the best template language in Python right now

Sep 20, 2006 13:35 · 269 words · 2 minute read

Christopher Lenz announced a new version of Genshi a couple of days ago. Genshi is the template language that was formerly known as Markup. A good way to think about it is that it’s Kid 2.0. The latest version includes a feature I was wishing for in Kid: plaintext templating. Take a look at the doctest for it:

>>> tmpl = TextTemplate('''Dear $name,
... We have the following items for you:
... #for item in items
...  * $item
... #end
... All the best,
... Foobar''')
>>> print tmpl.generate(name='Joe', items=[1, 2, 3]).render('text')
Dear Joe,
We have the following items for you:
 * 1
 * 2
 * 3
All the best,

This uses all of Genshi’s infrastructure, so the kinds of expressions and things that you’re used to when generating HTML and XML will still work here. HTML and XML definitely remain Genshi’s forte and if you’re not generating those formats at all, you’d likely want to choose a different language. (You can get higher performance when you don’t care about document structure…)

Genshi builds on Kid’s template language with flexible includes via xinclude, template matching via XPath expressions, much better error reporting and better performance to boot. The Trac project is currently in the process of converting over to Genshi, and it’s in plan that TurboGears will convert to Genshi in TG 1.1. (You can, of course, use Genshi now with TurboGears, and you’ll certainly be able to continue using Kid.)

Genshi owes a lot to Kid’s original design, but it has definitely moved the state of the art forward a good deal.