Genshi: the best template language in Python right now

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.

12 thoughts on “Genshi: the best template language in Python right now”

  1. good work. Sadly, you chose rather poor name for it.

    When I heard about other option, “papyrus”, I thought: “what a great name. But obviously geeks are not humble enough to understand that good name is important, and they will keep this stupid genshi thing”. And sadly I was right.

    Good language with poor name. Like the previous one, Kid. Doh.

  2. Kevin a question,
    how do you specify in TG with the @template decorator that a TextTemplate should be used? Or genshi will automatically know what template to use for (template=”genshi:.templates.text”) ?

  3. Damjan: unfortunately, Genshi text templates are currently not available through the TurboGears plugin mechanism.

    We have discussed how this could be implemented, and one obvious solution would be to actually provide *two* plugin classes (one for markup templates and one for text templates). Then you’d use something like “” to specify the template in @expose. That doesn’t seem all that elegant.

    The other option may be to switch to text templates implicitly if the output format is set to “text”. I’m not sure whether that’s a good idea, as it is not really explicit.

    Input from TG wizards very much welcome!

    In any case, better support for Genshi in TG is going to require some improvements of the plugin API in the long term, such as adding search paths, not having to hardcode the filename extension, and some way of selecting a template type.

  4. Genshi looks like a step forward in a lot of ways, the the use of XPath in match templates looks like a step backwards to me. I already have lots of match rules that look hard to implement with XPath, starting with:

    def has_class(item, classname):
    return classname in (item.attrib.get(‘class’, ”).split(‘ ‘))

    XPath doesn’t seem to handle the fact that class is actually a space-separated list of class names very well.

  5. Christopher: I agree that the plugin API needs improvements (I think everyone does :). I’d go with the separate plugin class route right now (genshi-text). It will still be relatively less used than “normal” genshi templates.

    Aaron: Perhaps what’s needed is an extension mechanism to easily add functions for the paths (probably with some common additions like the use of split that you’re suggesting).

    I like the XPath syntax in general for what it specifies, but I agree that there are cases where you need to select something outside the realm of what XPath gives you.

  6. Aaron, I see a number of ways to address that requirement:

    (a) Genshi could add support for the XPath 2 matches() function, which would allow you to match a class name on word boundaries using regular expressions.

    (b) Providing an extended XPath engine that would also support the “elem#id” and “elem.class” syntax that is well-known from CSS selectors. Similar to what jQuery does.

    Please do post any match template examples that aren’t well suited for XPath to the Genshi mailing list, so we can work on addressing them.

  7. I’ve totally fallen in love with Genshi! I learned it between midnight and 2AM one night. By the next day, I was totally productive and totally loving it! I like the fact that template inheritance works so easily, and I love the XPath stuff. It’s weird–Genshi is like a superset of all the templating engines, but in a way that is conceptually simple and elegant.

  8. Actually, Genshi is a good name, mostly because it can be easily googled with little room for ambiguity and it rolls off the tongue rather nicely. Think about it, why did Amazon go with THAT name!!! What on earth does a river in south america have to do with selling books online?!!
    So, if Genshi really takes off, it WILL be a good name, by definition.
    And it was probably fairly easy to get the domain.

Comments are closed.