Should I use Mako for templating?

I am considering a solution for templates, although my options are between Mako and Genshi. I find Genshi's templates a little ugly, so I turn more to Mako.

I thought: what's so good about Mako allowing Python inline code? How convenient is it for the average joe?

Wouldn't JUST patterns suffice without Python inline code?

+7
python template-engine mako genshi
source share
5 answers

As the mako homepage shows, the advantages of Mako are pretty clear: insanely fast, instantly familiar to anyone who comes in handy with Python in terms of both syntax and functions.

Genshi chooses "interpretation" instead of forward generation of Python code (according to their FAQ , which is for clarity of message error) and the "arm length" approach to Python (for example, using xpath for selectors, xinclude instead of inheritance, etc.), so this may be more natural for people who do not know Python, but are very competent with XML.

So what is your "audience"? If Python programmers, I suggest Mako (for speed and dating); if XML experts are inconvenient for Python, Genshi might be better suited (for "arm length from Python" and a closer match with the XML culture).

You mention "middle Joe", but Joe does not know Python, and xpath is a deep dark secret for him; if it was your audience, other template systems like Django might be better suited (help him avoid the troubles;).

+18
source share

Wouldn't JUST patterns suffice without Python inline code?

Only if your template language has sufficient logical functionality is, in fact, the scripting language itself. At this point, you could also use Python.

More involved sites often require complex presentation logic and non-trivial template structures, such as sections repeating in different places / pages and recursive trees. It is not fun if your template language binds your hands behind your back, because it takes a religious position that is "code in a BAD template."

Then you just finish writing presentation helper functions in your Python business logic, which is the worst combination of presentation logic and application you had to get started. Languages ​​that take power away from you because they do not trust you to use it with taste are lame.

+15
source share

This seems like a bit of a religious issue. Django templates take a tough stance: there is no code in the templates. They do this because of their history as a system used in stores where there is a clear separation between those who write code and those who create pages. Others (perhaps you) do not make such a clear distinction and feel more comfortable with a more flexible line between layout and logic.

It really depends on the taste.

+2
source share

Genshi is conceived (read: biased, optimized) to generate XML documents (even if it offers support for creating any text document). Mako and Django templates are designed as a universal text template system. Evoque , but with one fundamental difference is that it only makes the design choice a permissive python expression in templates, that is, without python statements.

One of the important results of this is that Evoque can evaluate the template in the sandbox, that is, you can safely provide untrusted users with write access to the template source code - a function that is almost impossible for template engines that also allow you to embed python instructions. Oh, and without losing a direct comparison of functions, Evoque is actually faster than Maco in some cases, and it also works in Python 3.

+2
source share

You could discipline yourself not to inject any Python code into the template, unless that is the last resort to do this work. I ran into a similar issue with the Django template, where I need to do some serious CSS gymnastics to display my content. If I could use some Python code in the template, it would be better.

0
source share

All Articles