Peter's Blog

Redefining the Impossible

Cheetah template


statcounter tells me this site is linked from the cheetah template website on the Who uses it page. Yes, I do use it, I like it a lot. Since I started using TurboGears I have been using kid but only because TurboGears didn't originally support Cheetah (Cheetah support is being added via template plugins).

What I like most about cheetah is that most of the time things just work the way you think they will and you can get on with your coding, instead of working out why things don't work. kid also gives me that feeling but cheetah is more powerful: kid is mainly aimed at xml/html, it can do plain text but cheetah is truly agnostic.

Kid and Cheetah share the ${variable} method of replacing text in the template with python expressions. This is very easy and powerful, although it goes against the doctrine of separating code from display. In django templates you can only substitute variables, you cannot call functions. I feel there is a fine line between python code that says 'object.name' and 'object.name()'. If you can explain one to a web designer, is it so hard to explain the other? Anyway, for my projects I am the web designer.

If there is one thing I would change about kid it would be to somehow add an else term to the if statement: I end up duplicating the test code and adding a not. The more duplication there is, the less maintainable it is.

Kid templates can be previewed as html very nicely which is useful in creating the layout of a site. If you preview kid templates as html you get substitute text where your dynamic content will go (e.g. 'Users name goes here') whereas cheetah would show the python expression that generates the content (e.g. '$(UserName}'). However, once you start working on the dynamic content you can only preview by passing through the template engine anyway. One of my projects generates a complex table, the columns and rows being generated dynamically. The kid preview is pretty useless.

Kid's other advantage is that it will gripe if your xhtml is not well formed, e.g. you miss out a closing tag. This has saved me a couple of times but there are other tools to validate your output.

Apparently there is a tantalising new version of cheetah out. Must have a look,


Filed under: cheetah kid python turbogears

Jacob Kaplan-Moss Says:

over 2 years ago

Actually, it's incorrect that Django templates cannot call functions. Django's variable substution "does the right thing" and uses a function call, a dictionary lookup, or a member lookup depending on what's appropriate. You can see the details at http://djangoproject.com/documentation/templates_python/#rendering-a-context

I should also point out that Django doesn't lock you into any particular template engine: you can use any engine you like (including Cheetah).

On preview: wow, pretty draconian comment-spam measures. Hope the link gets through OK.

Peter Says:

over 2 years ago

Thanks for putting me right.

The link got through fine. I'm still being hammered by comment spam but none has got through yet.

Peter

Have Your Say

I welcome constructive comments or questions but I reserve the right to delete any comments that displease me.

Who are you?

(Optional) If you enter an email address here I might email you back. Your email address will not be sold to spammers or shown anywhere

What do you have to say?