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:
-
Attribute based
TAL and Kid consist entirely of attributes and no elements. It latches onto whatever markup you’re already dealing with. I like this approach as opposed to XSLT’s verbose element model because it results in a lot less clutter. TAL documents are ~80% primary markup and 20% TAL markup. XSLT documents are ~80% XSLT markup and 20% primary markup.
-
Tool Friendliness
TAL survives round-trips through most tools. This is important.
The things I left:
-
I liked TALES at first but have come to point where I think having different types of expressions (
python
,path
,string
, etc.) just gets in the way. Python expressions do the job just fine (replace “/” by “.” and you usually come out okay). -
Maybe I just don’t get it but I was hoping for more of an XSLT
<template>
type construct or even a taglib approach. METAL is overly complex and doesn’t seem to do much other than insert stuff. I really want to be able to build up my own element definitions and have something to process them.
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:
-
Templating
Where XSLT has always shined for me was simple transformation based on matching element names in the source document and then turning them into something else in the output. In other words, making up your own tags or modifying the behavior of existing tags.
Kid will have some kind of facility to provide a rough equivalent of XSLT’s
<template>
functionality. -
Attribute Interpolation
I always wished TAL would have gone this route instead of having
tal:attributes
.
A few things I dislike about XSLT and will be trying to avoid:
-
It’s silly complex for simple templating.
-
The whole world isn’t available in XML just yet. Anything not XML is off-limits without extension functions.
-
Once you start using the
document()
xpath function, your happy world begins crashing down upon you. I still can’t explain this all that clearly but every time I’ve tried to composite documents using thedocument()
function, complexity increases significantly.
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.