🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
如果你之前使用 Django 模板,你应该会发现跟 Jinja2 非常相似。实际上, 很多的语法元素看起来相同,工作也相同。 尽管如此, Jinja2 提供了更多的在之前文档中描述的语法元素,并且某些 工作会有一点不一样。 本节介绍了模板差异。由于 API 是从根本上不同,我们不会再这里赘述。 ### 方法调用[](http://docs.jinkan.org/docs/jinja2/switching.html#id3 "Permalink to this headline") 在 Django 中,方法调用是隐式的。在 Jinja2 中,你必须指定你要调用一个对象。如 此,这段 Django 代码: ~~~ {% for page in user.get_created_pages %} ... {% endfor %} ~~~ 在 Jinja 中应该是这样: ~~~ {% for page in user.get_created_pages() %} ... {% endfor %} ~~~ 这允许你给函数传递变量,且宏也使用这种方式,而这在 Django 中是不可能的。 ### 条件[](http://docs.jinkan.org/docs/jinja2/switching.html#id4 "Permalink to this headline") 在 Django 中你可以使用下面的结构来判断是否相等: ~~~ {% ifequal foo "bar" %} ... {% else %} ... {% endifequal %} ~~~ 在 Jinja2 中你可以像通常一样使用 if 语句和操作符来做比较: ~~~ {% if foo == 'bar' %} ... {% else %} ... {% endif %} ~~~ 你也可以在模板中使用多个 elif 分支: ~~~ {% if something %} ... {% elif otherthing %} ... {% elif foothing %} ... {% else %} ... {% endif %} ~~~ ### 过滤器参数[](http://docs.jinkan.org/docs/jinja2/switching.html#id5 "Permalink to this headline") Jinja2 为过滤器提供不止一个参数。参数传递的语法也是不同的。一个这样的 Django 模板: ~~~ {{ items|join:", " }} ~~~ 在 Jinja2 中是这样: ~~~ {{ items|join(', ') }} ~~~ 实际上这有点冗赘,但它允许不同类型的参数——包括变量——且不仅是一种。 ### 测试[](http://docs.jinkan.org/docs/jinja2/switching.html#id6 "Permalink to this headline") 除过滤器外,同样有用 is 操作符运行的测试。这里是一些例子: ~~~ {% if user.user_id is odd %} {{ user.username|e }} is odd {% else %} hmm. {{ user.username|e }} looks pretty normal {% endif %} ~~~ ### 循环[](http://docs.jinkan.org/docs/jinja2/switching.html#id7 "Permalink to this headline") 因为循环与 Django 中的十分相似,仅有的不兼容是 Jinja2 中循环上下文的特殊变 量名为loop 而不是 Django 中的 forloop 。 ### 周期计[](http://docs.jinkan.org/docs/jinja2/switching.html#id8 "Permalink to this headline") Jinja 中没有 {% cycle %} 标签,因为它是隐式的性质。而你可以用循环对象 的 cycle 方法实现几乎相同的东西。 下面的 Django 模板: ~~~ {% for user in users %} <li class="{% cycle 'odd' 'even' %}">{{ user }}</li> {% endfor %} ~~~ Jinja 中看起来是这样: ~~~ {% for user in users %} <li class="{{ loop.cycle('odd', 'even') }}">{{ user }}</li> {% endfor %} ~~~ 没有与 {% cycle ... as variable %} 等价的。