In search of a Pythonic, XML-based Templating Language

I’ve been searching for the perfect Python based XML template language. I was happy to find TAL (and specifically, SimpleTAL) a while back but, although neither of us wants to admit it, we’ve been growing apart for some time now. I spent last week looking for options and, after careful consideration and planning (read: beer and a nap), decided to just build the XML template language I really wanted.

There’s at least four billion and nine text based template languages for Python but there aren’t a lot of options that fit nicely into the XML tool-chain. Or, if they do fit nicely into the XML tool-chain, they don’t fit nicely with Python.

My dreamboat XML template language would combine the pythonicness and simplicity of PTL, the templating features and pipeline-ability of XSLT, and the terseness of Zope’s TAL. I’m building it, it’s called Kid, and I’m making good progress to be honest.

But I have this overwhelming NIH feeling so I’ve decided the best thing to do is to run through the current set of tools and take a professional, objective look at why each isn’t getting it done for me (i.e. make fun of minor flaws and limitations until I feel better about myself). Herewith, a look at the good and the bad in the Python XML templating space…

TAL (Template Attribute Language)

I like TAL and have used it fairly extensively for XML templating. In fact, the current Kid Language Spec looks nefariously like TAL sans METAL and TALES.

The things I kept:

The things I left:

XSLT (XSL Transformations)

XSLT is the strange lover you can’t seem to leave behind. While I’m infatuated with the language from a theoretical point-of-view, I can’t stand typing all that crap over and over and over again. It’s too verbose, and the lack of a clear extension mechanism makes it hard to do things outside of document in, transform, document out.

That being said, I like XSLT. James Clark is my hero and I would totally have his babies if I was biological capable of producing them. Concepts I will be stealing from XSLT are:

A few things I dislike about XSLT and will be trying to avoid:

PXTL (Python XML Template Language)

I have very little experience with PXTL but a run through the language specification gave me the impression that it was stunningly thorough, correct, and quite complete. It is a lot of stuff though. I just can’t see this being picked up by someone quickly due to the large number of constructs and apparatus.

PTL (Python Template Language)

Quixote’s PTL is cool but is text based so it doesn’t really satisfy the basic requirements. There is one thing I really like about PTL that I was able to bring over into Kid: the ability to compile templates down to native Python modules, import them, use them like normal objects, etc. Kid does the same thing with XML documents. In fact, I ripped a ton of nice import hook code from Quixote to add the ability to import Kid templates without having to pre-compile them. They work just like .py files in that they are compiled the first time they are imported and written to a .pyc file.

I have a feeling Quixote with PTL for text based media types and Kid for XML based media types will make a really solid, simple, pythonic web framework.