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?

This blog post was posted to It's All Geek to Me on Thursday February 28th, 2008 at 7:15PM
 

8 Responses to "Practicality versus Purity for Python Templates"

  • February 28th, 2008, 11:18 p.m.

    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.

  • February 29th, 2008, 1:18 a.m.

    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)

  • Doug Napoleone
    February 29th, 2008, 2:09 a.m.

    @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)

  • February 29th, 2008, 4:31 a.m.

    "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.

  • Doug Napoleone
    February 29th, 2008, 5:42 a.m.

    @Dean:

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

  • whity
    February 29th, 2008, 4:59 p.m.

    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.

  • November 18th, 2010, 2:15 p.m.

    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.

  • April 12th, 2012, 2:35 p.m.

    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.

Leave a Comment

You can use bbcode in the comment: e.g. [b]This is bold[/b], [url]http://www.willmcgugan.com[/url], [code python]import this[/code]
Preview Posting...
Previewing comment, please wait a moment...
Will McGugan

My name is Will McGugan. I am an unabashed geek, an author, a hacker and a Python expert – amongst other things!

You are reading my tech blog. See the homepage for my other blogs.

Search for Posts
Possibly related posts
Tags
Popular Tags
 
Archives
2013
 
Recent Comments
Sir, can you give me full code.?
Trying to implement this code. What are the requirements? Does it need a certain version of PHP, etc? Do I ...
Hi Will Can't seem to run any pyopengl applications on my Win7 64bit setup. Using python 3.3 but keep getting ...
Great work, is it possible to jump to a specified picture?
Thanks for the lesson and library, but: What if I wanted to limit the frames as well? Is it really ...
- The novice on Master time with PyGame
 
© 2008 Will McGugan.

A technoblog blog, design by Will McGugan