Python Mako 还是 Jinja2?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3435972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:05:46  来源:igfitidea点击:

Mako or Jinja2?

pythontemplatestemplate-enginemakojinja2

提问by ychaouche

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

我没有找到 jinja2 和 Mako 的很好比较。你会用什么来完成什么任务?

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

我个人对 mako 感到满意(在 pylons web 应用程序上下文中),但很想知道 jinja2 是否有一些不错的功能/改进,而 mako 没有?- 或者可能是缺点? -

采纳答案by Jesse Dhillon

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

我个人更喜欢 Jinja2 的语法而不是 Mako 的。以Mako 网站上的这个例子为例

<%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 fauxXML where some directives close like tags and have attributes?

这里有太多的结构,我什至必须在开始之前查阅文档。哪些标签以 开头<%和结尾/>?其中哪些可以关闭%>?当我想输出一个变量 ( ${foo})时,为什么还有另一种输入模板语言的方法?这个人造XML 有什么用,其中一些指令像标签一样关闭并具有属性?

This is the equivalent example in Jinja2:

这是 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:

Jinja2 有过滤器,我听说 Mako 也有,但我没见过。过滤器函数不像常规函数那样工作,它们采用被过滤值的隐式第一个参数。因此,在 Mako 中,您可能会这样写:

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

That's horrible. In Jinja2 you would write:

那太糟了。在 Jinja2 中你会写:

{{ 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.

在我看来,Jinja2 示例非常具有可读性。Jinja2 更规则,因为标签以可预测的方式开始和结束,要么{% %}用于处理和控制指令,要么{{ }}用于输出变量。

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!

但这些都是个人喜好。我不知道有什么更重要的理由选择 Jinja2 而不是 Mako,反之亦然。并且 Pylons 足够好,您可以使用它们!

Updateincluded 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!

更新包括 Jinja2 宏。尽管无论如何都是人为的,但在我看来,Jinja2 示例更易于阅读和理解。Mako 的指导思想是“Python 是一种很棒的脚本语言。不要重新发明轮子……您的模板可以处理它!” 但是 Jinja2 的宏(实际上是整个语言)看起来更像 Mako 所做的 Python!

回答by Python Best Friend

Take a look at wheezy.templateexample:

看看wheezy.template 的例子:

@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 hereand here), well testedand documented.

它针对性能进行了优化(更多在这里这里),经过充分测试记录