Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mako or Jinja2? [closed]

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

like image 617
ychaouche Avatar asked Aug 08 '10 20:08

ychaouche


People also ask

Is Jinja2 open source?

Jinja Template — VoltVolt is a free and open-source Bootstrap 5 Admin Dashboard featuring over 100 components, 11 example pages and 3 customized plugins.

What are Mako files?

Mako is a template library written in Python. It provides a familiar, non-XML syntax which compiles into Python modules for maximum performance. Mako's syntax and API borrows from the best ideas of many others, including Django and Jinja2 templates, Cheetah, Myghty, and Genshi.

What is Mako language?

Mako is a template library written in Python. Mako is an embedded Python (i.e. Python Server Page) language, which refines the familiar ideas of componentized layout and inheritance. The Mako template is used by Reddit. It is the default template language included with the Pylons and Pyramid web frameworks. Mako.

What is Mako software?

The Mako System is a PCI-certified, cloud-managed, carrier-independent networking solution that makes it easy to connect and manage thousands of global sites in a high-performance business network that is secure, reliable, scalable and cost-effective.


1 Answers

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!

like image 161
Jesse Dhillon Avatar answered Sep 23 '22 21:09

Jesse Dhillon