views:

957

answers:

5

I've been considering a templating solution, although my choices are between Mako and Genshi. I find templating in Genshi a bit ugly, so I'm shifting more towards Mako.

I've gone to wonder: what is so good about the fact that Mako allows embedded Python code? How is it convenient for the average joe?

Wouldn't templating JUST suffice without having embedded Python code?

+1  A: 

This seems to be a bit of a religious issue. Django templates take a hard line: no code in templates. They do this because of their history as a system used in shops where there's a clear separation between those who write code and those who create pages. Others (perhaps you) don't make such a clear distinction, and would feel more comfortable having a more flexible line between layout and logic.

It really comes down to a matter of taste.

Ned Batchelder
+9  A: 

As the mako homepage points out, Mako's advantages are pretty clear: insanely fast, instantly familiar to anyone who's handy with Python in terms of both syntax and features.

Genshi chooses "interpretation" instead of ahead-of-time Python code generation (according to their FAQ, that's for clarity of error messages) and an "arm's length" approach to Python (e.g. by using xpath for selectors, xinclude instead of inheritance, etc) so it might be more natural to people who know no Python but are very competent with XML.

So what's your "audience"? If Python programmers, I suggest Mako (for speed and familiarity); if XML experts that are uncomfortable with Python, Genshi might be a better fit (for "arm's length from Python" approach and closer match to XML culture).

You mention "the average Joe", but Joe doesn't know Python AND xpath is a deep dark mystery to him; if THAT was really your audience, other templating systems such as Django's might actually be a better fit (help him to avoid getting in trouble;-).

Alex Martelli
A: 

You could discipline yourself to not inject any Python code within the template unless it's really the last resort to get the job done. I have faced a similar issue with Django's template where I have to do some serious CSS gymnastics to display my content. If I could have used some Python code in the template, it would have been better.

Thierry Lam
Luckily, it's very easy to use Mako in Django, if you're so inclined (http://code.google.com/p/django-mako/ among other places)
Gregg Lind
A: 

Genshi is conceived (read: biased, optimized) for generation of xml docs (even if it does offer support for generating any kind of text document). Mako and Django templates are conceived as generic text templating system. Evoque also, but with one fundamental difference that it makes the design choice to only allow python expressions in templates i.e. no python statements.

One important net result of this is that Evoque is able to execute template evaluation in a sandbox -- i.e. you could safely give untrusted users write-access to a template's source code -- a feature that is virtually impossible for template engines that also allow embedding of python statements. Oh, and while not losing out any in a direct feature comparison, Evoque is in some cases actually faster than Mako, and it also runs on Python 3.

Mario Ruggier
+2  A: 

Wouldn't templating JUST suffice without having embedded Python code?

Only if your templating language has enough logical functionality that it is essentially a scripting language in itself. At which point, you might just as well have used Python.

More involved sites often need complex presentation logic and non-trivial templated structures like sections repeated in different places/pages and recursive trees. This is no fun if your templating language ties your hands behind your back because it takes the religious position that "code in template are BAD".

Then you just end up writing presentation helper functions in your Python business logic, which is a worse blending of presentation and application logic than you had to start with. Languages that take power away from you because they don't trust you to use it tastefully are lame.

bobince