I didn't find a good comparison of jinja2 and Mako. What would you use for what tasks ?

I personnaly was satisfied by mako (in a pylons web app context) but am curious to know if jinja2 has some nice features/improvements that mako doesn't ? -or maybe downsides ?-


I personally prefer Jinja2's syntax over Mako's. Take this example from the Mako website

<%inherit file="base.html"/>
<%
    rows = [[v for v in range(0,10)] for row in range(0,10)]
%>
<table>
    % for row in rows:
        ${makerow(row)}
    % endfor
</table>

<%def name="makerow(row)">
    <tr>
    % for name in row:
        <td>${name}</td>\
    % endfor
    </tr>
</%def>

There are so many constructs here that I would have to consult the documentation before I could even begin. Which tags begin like <% and close with />? Which of those are allowed to close with %>? Why is there yet another way to enter the template language when I want to output a variable (${foo})? What's with this faux XML where some directives close like tags and have attributes?

This is the equivalent example in Jinja2:

{% extends "base.html" %}

<table>
  {% for row in rows %}
    {{ makerow(row) }}
  {% endfor %}
</table>

{% macro make_row(row) %}
  <tr>
    {% for name in row %}
      <td>{{ name }}</td>    
    {% endfor %}
  </tr>
{% endmacro %}

Jinja2 has filters, which I'm told Mako also has but I've not seen them. Filter functions don't act like regular functions, they take an implicit first parameter of the value being filtered. Thus in Mako you might write:

${escape(default(get_name(user), "No Name"))}

That's horrible. In Jinja2 you would write:

{{ user | get_name | default('No Name') | escape }}

In my opinion, the Jinja2 examples are exceedingly more readable. Jinja2's more regular, in that tags begin and end in a predictable way, either with {% %} for processing and control directives, or {{ }} for outputting variables.

But these are all personal preferences. I don't know of one more substantial reason to pick Jinja2 over Mako or vice-versa. And Pylons is great enough that you can use either!

Update included Jinja2 macros. Although contrived in any case, in my opinion the Jinja2 example is easier to read and understand. Mako's guiding philosophy is "Python is a great scripting language. Don't reinvent the wheel...your templates can handle it!" But Jinja2's macros (the entire language, actually) look more like Python that Mako does!


Take a look at wheezy.template example:

@require(user, items)
Welcome, @user.name!
@if items:
    @for i in items:
        @i.name: @i.price!s.
    @end
@else:
    No items found.
@end

It is optimized for performance (more here and here), well tested and documented.