Genshi smells like PHP?

29 January 2007

In Python template engines - why reinvent PHP?, Cliff Wells writes:

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.

At least with Genshi and Kid, that “ugly thing to describe web pages with” is pretty much just XHTML. And that, of course, is a feature. For a web developer, reading and writing HTML (as well as CSS and Javascript) is at the core of any front-end work. I don't want a template system that tries to hide the HTML from me; rather, it should enable me to just take some HTML and hook it up to server-side application code.

But that has little to do with PHP. Sure, PHP makes you mix code with HTML markup. And most template engines, including Genshi, enable you to add variable substitution and some level of control flow to HTML markup. The problem with PHP, however, is not so much that it allows mixing code and markup, but rather that it uses a document-centric approach where your HTML “templates” drive the application code, and not the other way around. (Plus that PHP is simply a badly designed language.)

Libraries that allow you to generate HTML programmatically—without a single angle bracket in sight—have existed as long as the web itself, but they have been mostly unsuccessful. S-expression style templating as implemented by Stan and now Breve are based on the same basic concept or desire, even though they do use the flexibility and power of Python expressions to provide a more convenient syntax.

In the end, the reason why I personally wouldn't use something like Stan or Breve is because I actually want to work directly with the HTML, CSS, and Javascript in my application. I want my text editor of choice to be able to assist me with all the tools it provides for working with markup, including support for embedded CSS and Javascript. I want to be able to quickly preview a template by opening it directly in the browser, without having to run it through the template engine first. When I'm working on a template, I want to be using HTML, not Python.