Python template engines - why reinvent PHP?
Filed under: breve template xml python stan dslWhen I first published TurboStan, I admit I was a bit taken aback by the apparent lack of interest from the majority of TurboGears users. Stan's beauty and clarity seemed so obvious to me that it baffled me that people would choose what I perceived as a lesser PHP over something as clearly elegant as Stan. I tallied the possible reasons for TurboStan's lack of fanfare and came up with the following:
- Not the default engine for TurboGears. While not a good technical reason, there are reasons for sticking close to the mainstream, especially with an unstable target like TurboGears was in the early days.
- Dependencies on Twisted, Nevow and zope.interfaces. At least one user mentioned that he was unable to find a version of zope.interfaces that would both install and was compatible with the other two dependencies. I figured that if one person mentioned this problem, then there were probably a dozen more who gave up in silence.
- Lack of documentation. This plagued me in my early days using Nevow and I didn't improve the situation for others any by extending Stan with even more features that were only documented briefly in my blog (and I changed blogs about as often as my socks in those days).
- Not really seeing the elegance of Stan.
Since the release of Breve, I've seen a bit more interest than I did with TurboStan (I even received a patch from someone, something that TurboStan never did), but still the warm reception isn't quite what I'd like it to be.
Now I admit that when I first decided to try Nevow (way back before TurboGears was a glimmer in Kevin's eye), I took a look at the two options for templating (Nevow's XML engine and Stan) and went with the XML option. Stan looked... odd. I didn't really see it for what it was. Plus I had some odd sense that XML was a better choice because it was more "standard". I'm not sure when the lights came on and I was able to appreciate the elegance of Stan, but at some point it happened and I'm now loathe to touch the angle brackets on my keyboard. I became so attached to Stan that I refused to try Django, Pylons, CleverHarold or any other framework that I couldn't use Stan with. The main reason I used TurboGears was so I could use TurboStan. Now, I'm sure at least part of this love came from the fact that I was playing with my own creation (not Stan, but the extensions I added) and my ability to direct its future. There's always a lot of fun in that and I can't deny that is part of it.
Still, as I watch TurboGears (and a few other frameworks) adopt Genshi as their default engine, I can't help but wonder at the attraction people find to these sorts of engines. I can't help but notice with a bit of smugness that there's a good chunk of helper functions in Pylons (and Rails) to alleviate the need to spell out HTML, which seems to confirm my belief that their default template engines don't really help as much as they might when it comes to HTML generation (an odd weakness for a web framework to have). When I was first starting on the web (and Python web frameworks were pretty ugly), I was forced to program in PHP/Smarty at my job. Once I got over the novelty of programming amongst the web's various vagaries and oddnesses, I bemoaned these tools and wished for a Python framework that would allow me to escape the ugliness I perceived in them. So what's my point? Well my point is that template engines such as Genshi, Kid, Myghty, et al all smell of PHP to me. Is some of that just cosmetic? You bet. In fact, probably most of it is just cosmetic. There's no doubt in my mind that Genshi is a powerful too and probably full of beautiful Python code under the hood. That doesn't change how it looks to me from the outside. It isn't Python. I find it mildly ironic that Pythonistas are renowned for their loathing of Perl's typographical perversions and then turn around and create equally ugly things to describe web pages with.
Anyway, this isn't so much about how much I dislike those types of engines as it is curiosity as to what reasoning (whether it be founded in technical or taste) other Python programmers have for selecting their preferred template engine. Going along with that, I'm curious why DSL's like Stan, Breve, XIST and others don't generate more enthusiasm amongst Pythonistas.
So if you've got an opinion on this, I'd be interested in hearing it. I'm not looking for an argument, just enlightenment (right now I feel like I'm the enlightened one looking out on the unwashed masses, but there's always the remote possibility I just don't get it, and if that's the case, someone should be kind enough to show me the light) . Also I'm interested to see if there is an actual perceived shortcoming in Breve so that I can perhaps address it.




Hi Cliff.
I'm comfortable with both s-expressions and XML, but it comes down to the fact that I prefer to template in something that looks like the final product. I've been writing HTML for over 10 years. I know exactly what I'm building and when someone else who also knows HTML looks at the pages, they know what I'm doing. I am the group's designer, so that's not an issue.
I do understand the implications of Breve, I've done s-expression template generation in Scheme. I also recommended Breve to a friend who has a XML->C#/Java code generation engine as a better way to generate the XML than hand authoring. In the case of code generation, there's no reason to write stuff in XML, you get no benefit of experience and can gain big code-reduction wins using s-expressions both in abstraction and syntax cleanliness.
I'm not a hardcore XML geek, but I'm probably one of the more XML-friendly Pythonistas. I've done a fair amount of XML processing in the past, including a content system using XSL-T (mess of a language) that was XML Schema (mess of a language) validated. I ultimately decided, like most of the Python community, that XML was a last-choice option. The upshot was that I learned xpath and got Vim set up as a passable XML editor (though no schema support).
The nice thing about Kid and Genshi is that the Python (real python!) bits provide me with enough flexibility to take a set of data that wasn't built specifically for the page (e.g. something I'm reusing elsewhere) and transform it into the output I want. This is important because it lets me keep the data manipulation code close to the formatting code and keeps my business logic clean from formatting cruft. Separation of concerns.
I could do that using a text-based engine, but I know enough about XML to know that you shouldn't hand-generate XML and the fact that Genshi and Kid work at the XML Infoset level provides a strong guarantee that the page will come out well-formed. Genshi has the second advantage in that I'm able to leverage my knowledge of xpath to compose pages (or parts of pages) without the included pages knowing anything about it. Basically Genshi is what XSL-T should have been but isn't. It's a superb tool for transforming XML and I'd use it for that even if it didn't output HTML.
While I do value brevity for ease in input/editing/reading, I have an extensive (as in couple thousand lines of code) set of vim macros for authoring XML, web pages, and Genshi/Kid templates, so it's . I can generate the XML equivalent of a Breve template in about the same number of keystrokes and considerably fewer when the language is HTML and I can leverage my macro expansions.
I'm excited about Breve but not for web page authoring in TG. If I didn't have as much time/knowledge invested in the XML family, I'd definitely consider it strongly.