Take the 2-minute tour ×

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 ?-

share|improve this question
add comment

closed as primarily opinion-based by Bill the Lizard Oct 19 '13 at 13:01


3 Answers

up vote 29 down vote accepted

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

<%inherit file="base.html"/>

    % for row in rows:
    % endfor

<%def name="makerow(row)">
    % for name in row:
    % endfor

update start

One of the removed construct here is the : <% ... python code ... %>. It was quite unfair to place it there since you should actually pass variable inside the context. But Mako still allows you to use plain python code within templates. It shouldn't be used often but when you really need it. It's there just to make your life easier. If you need something in one template but not anywhere else and Passing it to the context is overkill. That's what you really need.

update end

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" %}

  {% for row in rows %}
    {{ makerow(row) }}
  {% endfor %}

{% macro make_row(row) %}
    {% for name in row %}
      <td>{{ name }}</td>    
    {% endfor %}
{% 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:

${user | get_name, default('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!

share|improve this answer
Not really fair: Your "equivalent in Jinja" excluded half the stuff from the Mako example and thus looks shorter. Mako's <% /> vs <% %> is not that confusing (blocks vs inline code). Mako has filter functions too and they look just the same. –  Jochen Ritzel Aug 8 '10 at 23:02
@Jesse: I do not like Jinja2's insistence on (nearly) replicating Python. You can't use any built-in functions, including len and enumerate, unless you pass them in as context variables. And using .__len__ or loop.index0 instead is ugly and unintuitive. –  Nikhil Chelliah Aug 13 '10 at 0:57
However, I agree that Jinja2 is generally cleaner, more customizable (e.g. you can change the syntax), and in my experience more forgiving regarding Unicode. –  Nikhil Chelliah Aug 13 '10 at 1:02
how can you compare jinja filters to mako filters, say they're "better", but "you've not seen" mako filters ? Seems hardly reasonable. We use practically the same syntax as Jinja for filters: ${user | get_name, default('No Name') , escape} . It's pretty obvious you've never used Mako which is perfectly fine but you're hardly in a position to make a reasonable comparison, or call our syntax "stupid", thanks for that ! –  zzzeek Nov 23 '10 at 3:59
I wrote this a few months ago and I've since read up on Mako filtering syntax. I never called it stupid but I do question many of the design decisions made by the Mako team. In my opinion, Jinja was designed to be learned as quickly as possible, with the fewest documentation trips required. Mako was not, and it exhibits an arbitrary and inconsistent syntax. I am in a position to judge Mako, as I have used it; enough that I decided that it's not for me. PS, a quotation mark is used to quote words actually said. You imply that I said one is better or one is stupid, but I said neither. –  Jesse Dhillon Nov 23 '10 at 19:29
show 9 more comments

I wrote an article about it at: http://williamferreira.net/blog/2010/12/28/mako-template-vs-jinja2/

share|improve this answer
THank you William, did you write an english version for it ? that would benefit a lot of people too. –  ychaouche Jan 7 '11 at 8:54
Google translation is readable. This is performance mostly. –  Alexy Jan 11 '13 at 21:07
add comment

Take a look at wheezy.template example:

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

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

share|improve this answer
add comment
