51

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

earl
  • 40,327
  • 6
  • 58
  • 59
ychaouche
  • 4,922
  • 2
  • 44
  • 52

2 Answers2

47

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!

Jesse Dhillon
  • 7,841
  • 1
  • 34
  • 34
  • 14
    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 08 '10 at 23:02
  • I said these are matters of personal preference, you are free to show how Mako's syntax makes more sense to you. I didn't include the definition of makerow because that's not part of a template in Jinja. – Jesse Dhillon Aug 09 '10 at 01:41
  • @Jesse -- to be fair, why not define and use a macro called `makerow` for the Jinja2 example? **See:** http://jinja.pocoo.org/2/documentation/templates#macros – Sean Vieira Aug 10 '10 at 02:03
  • Also, +1 for Jinja2 -- I too prefer its syntax. – Sean Vieira Aug 10 '10 at 02:09
  • I added the Jinja2 macros, thanks Sean. – Jesse Dhillon Aug 10 '10 at 07:12
  • 14
    @Jesse: I do *not* like Jinja2's insistence on (nearly) replicating Python. You can't use any [built-in functions](http://docs.python.org/library/functions.html), including `len` and `enumerate`, unless you pass them in as context variables. And using `.__len__` or `loop.index0` instead is ugly and unintuitive. – Nikhil Aug 13 '10 at 00:57
  • Mako filters are simple - just use any function that you've defined or imported. – Nikhil Aug 13 '10 at 01:02
  • 1
    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 Aug 13 '10 at 01:02
  • @Nikhil, agreed re: `loop.index0` business. Not sure why having to specifically import built-ins into the environment matters: it's a one-line declaration to add the ones which matter to you one-by-one. Although I do frequently get confused by whether I should be using `true` or `True` in a given situation, which is dumb (I think it doesn't matter). – Jesse Dhillon Aug 13 '10 at 05:55
  • 56
    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 03:59
  • 10
    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
  • row {{ makerow(row) }} should be {{ make_row(row) }} – Ib33X Oct 05 '11 at 08:53
  • 2
    @JesseDhillon: Actually, you did say "The example you provide is further proof that Mako suffers from syntactic stupidity". Anyway, as PyCharm is adding support for both Mako and Jinja2 in version 2.0, perhaps we should get the verdict on syntax complexity from their developers :D – sayap Oct 29 '11 at 01:15
  • 2
    Did some edit to make the comparison match Jinja2. My guess is that mako is way more awesome. I mean, if you had jinja2 templates and want to convert to mako, you can make a precompiler that will understand jinja2 syntax and makes it work inside mako. By the way, mako syntax is more pythonic than jinja2. – Loïc Faure-Lacroix Oct 07 '12 at 13:44
8

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.

Michael Platings
  • 3,045
  • 2
  • 25
  • 17
  • 1
    It looks nice, but the version available on pip is quite outdated: https://pypi.python.org/pypi/wheezy.template – nowox Mar 07 '18 at 13:35
  • smx is the the smallest code base, easy to understand and extend: https://pypi.org/project/smx/0.8.4/ `%define(make_row, %for(name in row, %name% ) ) %set(body, %for(row in rows, %makerow% )
    ) %expand(%include(base.html)) `
    – Erik Aronesty Mar 12 '19 at 13:55