February 28, 2008 will

Practicality versus Purity for Python Templates

There are a number of very powerful template languages available in Python. Some template languages, such as Genshi, allow complex Python statements and even full Python code within the template file. Others, such as Django templates, prefer to restrict templates to presentation only and do not allow general Python expressions within the body of the template.

In the context of a web framework, is it better to have the full expressiveness of Python, or restrict templates to presentation only?

Use Markdown for formatting
*Italic* **Bold** `inline code` Links to [Google](http://www.google.com) > This is a quote > ```python import this ```
your comment will be previewed here
gravatar
Jay Pipes

I think this is a good question. Personally, I think a balance of restricted functionality with a focus on presentation in the templates is the right way to go, but YMMV.

gravatar
Tim Parkin

Personally I'm coming to the conclusion that there should be two layers within a template. A data preparation layer and a presentation layer. The presentation layer should be a series of simple format, loop, insert statements. The data preparation layer should be pure python with tools.

So a template becomes a combination of pure python and a simple xhtml template. I like Genshii in that it allows you to do this if you want. I like TAL like templates because they enforce a certain separation (but do we python programmers like enforced rules instead of conventions). The worst template language in my eyes is Django for it's creation of a new language (Why do template designers inist on recreating the wheel like that)

gravatar
Doug Napoleone

@Tim:

Question: why not separate out the data preparation logic into a separate file?
This would make things DRY in that the same presentation logic could be used for multiple preparations and the same preparation logic could be used for multiple presentations.

Granted, then you would have Django views and templates, where the templates are just the presentation layer consisting of 'a series of simple format, loop insert statements.'

As for inventing your own restricted language there are MANY MANY reasons for doing so:

1. speed.

It can be costly it allow for generic python code in a text file, as passing it off to be interpreted can be an added cost. this has to be weighed against the new language overheads of course.

2. complexity.

Complexity to debug. Complexity to optimize. Complexity to understand where things are happening. This can happen in any template system, but try debugging your generator expressions in a Genshi template, and you quickly go insane. (NOTE: Django is not much better here...) the rule is don't do anything more complex than simple looping and insertions in your templates, and put the complex stuff in your python irregardless of the template language. Why is the template language supporting all those complex operations again?

3. security

by restricting the command set in a template language you can add a level of security. many template languages use sand boxing and other systems for this (Neither Genshi nor Django do this, but it is a reason why people keep inventing new template languages)

gravatar
Dean Landolt

"We're all adults here" right? We all know not to put too much code in the templates, and most of us know why. But to completely strip the ability to do such a thing doesn't seem very pythonic to me.

gravatar
Doug Napoleone

@Dean:

Which template system completely strips the ability to put code in it?
Just curious.

gravatar
whity

I personally like Mako, because it's simple and they did not reinvented the wheel, allowing me to do some python if it's really needed.
I think there no better one, it depends on how you like to structure your work.

gravatar
Joss
Web programming is disjointed enough already. For anything beyond the simplest possible projects one has to deal with html/css/javascript/sql + the server language. That's 5 languages already. If I have to learn a 6th language, no matter how simple, there had better be a bloody good reason for it. Stopping people from shooting themselves in the foot isn't good enough.. there are many circumstances where one has code that is entirely about presentation, and that code belongs as close possible to the html/javascript bits. Overly simplistic template languages get in the way… so, yes, I want full python inside templates and if I shoot myself in the foot because of that, that's my problem.
gravatar
Rachat de prêts
Question: why not separate out the data preparation logic into a separate file?
This would make things DRY in that the same presentation logic could be used for multiple preparations and the same preparation logic could be used for multiple presentations.