Home Login Create account Contact Help
Close

Login to your account

Other Options

Tags: Programming Python Templates Web

Practicality versus Purity in Python templates

There are a number of very powerful template languages available in Python. Some template languages, such as Genshi [genshi.edgewall.org], allow complex Python statements and even full Python code within the template file. Others, such as Django [djangoproject.com] 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?

...

NB. You can post code with the [code] bbcode tag. Many languages are supported. e.g.

[code python]

print "Hello, World!"

[/code]

...

This debate was inspired by this blog post [regebro.wordpress.com]

Related debate: XML or Text for Python Templates. [becontrary.com]



Log in to post an argument Subscribe to feed
Rank order
Expressive Python
Better to allow arbitrary Python code in templates
dazza argues...
Thu, 28 Feb 2008 by dazza
Rank 1. Average +0.2 ( 4 votes )
Expressive python can cause a lot of problems. You can tie yourself in a knot and poor, lazy or rushed programmers can undermine the project. But I'd rather have that then a restrictive presentation layer that doesn't allow the team to deliver the project.
Lennart Regebro argues...
Thu, 28 Feb 2008 by regebro
Rank 2. Average -0.2 ( 4 votes )
Expressive templates lets you be lazy.
Expressive templates makes for messy and unmaintanable code. Well, not all code needs to be. Expressive templates lets you be lazy.
Glyph Lefkowitz argues...
Thu, 28 Feb 2008 by glyph
Rank 3. Average -1.0 ( 3 votes )
Templates are Python programs to generate HTML output. Just because they provide a convenient syntax for outputting large blocks of text doesn't mean they should cripple the expressive power of the language. If I want to write a "for" loop in a template, why should I have to mark up a section of my template with some weird, custom tags, and then write the actual "for" loop somewhere else that's hard to find?
Christopher Perkins argues...
Fri, 29 Feb 2008 by percious
Rank 4. Average -1.0 ( 2 votes )
I think allowing Python code directly in a template is a good thing, and in fact, I refuse to use any templating engine that does not allow it.

I believe in the MVC paradigm, and being able to put code directly into your View keeps a lot of garbage out of your controller. Take for instance the concept of pagination. In reality, I just want my template to understand, the current page, how many records per page, and how many records there are in total. The View should be able to figure out how that information should be rendered. Doing these kind of manipulations without a block of python code gets to be pretty tedious and difficult, but if you move the logic for viewing the links for pagination into it's own block it becomes much more understandable and readable.

In summary, any time you have a view which needs more complex algorithms to render, having a python block is not only useful, but preferred.
Will McGugan argues...
Sat, 01 Mar 2008 by admin
Rank 5. Average 0.0 ( 1 votes )
Adding code to templates allows you to adjust functionality in your web app on the fly
Adding code to templates allows you to adjust functionality in your web app on the fly -- since most frameworks will reload the template. Modifying the app code usually requires a restart of the server, which can lead to a brief service outage.
Presentation only
Templates should be purely for presentation
Lennart Regebro argues...
Thu, 28 Feb 2008 by regebro
Rank 1. Average +0.6 ( 5 votes )
Some lazy guy will sooner or later start putting in python code with logic in those clean pretty templates.
Mixing layout and logic makes for messy and complex code, that is hard to debug and maintain. So you need to separate logic and presentation. You can do this by convention if you are by yourself, but in a project where you are many, convention is not enough. Some lazy guy will sooner or later start putting in python code with logic in those clean pretty templates.

So you need a template language that simply does not let them do that.
Glyph Lefkowitz argues...
Thu, 28 Feb 2008 by glyph
Rank 2. Average +1.0 ( 4 votes )
Templates are a resource that should be loaded and manipulated by code. Ideally you should be able to load that resource from an untrusted source, for example to allow users to customize their own presentation.

Asking whether you should be able to put Python in your templates is like asking whether you should be able to put Python in images or sound files. The expressive power of Python is fabulous when you're writing programs and should always be close at hand, but Python code goes in Python modules, so it can be found, loaded, inspected, tested, and documented. Instead of asking Django "why do you make it so hard to put Python code in HTML templates", we should ask Genshi "why do you make it so hard to put Python code in Python modules"? It seems like it should always be easier to do the latter.