jinja2模板设计二:测试、注释、空白、转义、行语句
一、测试
除了过滤器,所谓的“测试”也是可用的。测试可以用于对照普通表达式测试一个变量。 要测试一个变量或表达式,你要在变量后加上一个 is 以及测试的名称。例如,要得出 一个值是否定义过,你可以用 name is defined ,这会根据 name 是否定义返回 true 或 false 。
测试也可以接受参数。如果测试只接受一个参数,你可以省去括号来分组它们。例如, 下面的两个表达式做同样的事情:
{% if loop.index is divisibleby 3 %}
{% if loop.index is divisibleby(3) %}
内置测试清单
- callable(object)
-
Return whether the object is callable (i.e., some kind of function). Note that classes are callable, as are instances with a __call__() method.
- defined(value)
-
Return true if the variable is defined:
{% if variable is defined %} value of variable: {{ variable }} {% else %} variable is not defined {% endif %}See the default() filter for a simple way to set undefined variables.
- divisibleby(value, num)
-
Check if a variable is divisible by a number.
- escaped(value)
-
Check if the value is escaped.
- even(value)
-
Return true if the variable is even.
- iterable(value)
-
Check if it’s possible to iterate over an object.
- lower(value)
-
Return true if the variable is lowercased.
- mapping(value)
-
Return true if the object is a mapping (dict etc.).
New in version 2.6.
- none(value)
-
Return true if the variable is none.
- number(value)
-
Return true if the variable is a number.
- odd(value)
-
Return true if the variable is odd.
- sameas(value, other)
-
Check if an object points to the same memory address than another object:
{% if foo.attribute is sameas false %} the foo attribute really is the `False` singleton {% endif %}
- sequence(value)
-
Return true if the variable is a sequence. Sequences are variables that are iterable.
- string(value)
-
Return true if the object is a string.
- undefined(value)
-
Like defined() but the other way round.
upper(value)
Return true if the variable is uppercased.
二、注释
要把模板中一行的部分注释掉,默认使用 {# ... #} 注释语法。
这在调试或 添加给你自己或其它模板设计者的信息时是有用的:
{# note: disabled template because we no longer use this
{% for user in users %}
...
{% endfor %}
#}
三、空白控制
默认配置中,模板引擎不会对空白做进一步修改,所以每个空白(空格、制表符、换行符 等等)都会原封不动返回。如果应用配置了 Jinja 的 trim_blocks ,模板标签后的 第一个换行符会被自动移除(像 PHP 中一样)。
此外,你也可以手动剥离模板中的空白。当你在块(比如一个 for 标签、一段注释或变 量表达式)的开始或结束放置一个减号( - ),可以移除块前或块后的空白:
{% for item in seq -%}
{{ item }}
{%- endfor %}
这会产出中间不带空白的所有元素。如果 seq 是 1 到 9 的数字的列表, 输出会是 123456789 。
如果开启了 行语句 ,它们会自动去除行首的空白。
提示:标签和减号之间不能有空白。
有效的:
{%- if foo -%}...{% endif %}
无效的:
{% - if foo - %}...{% endif %}
四、转义
有时想要或甚至必要让 Jinja 忽略部分,不会把它作为变量或块来处理。例如,如果 使用默认语法,你想在在使用把 {{ 作为原始字符串使用,并且不会开始一个变量 的语法结构,你需要使用一个技巧。
最简单的方法是在变量分隔符中( {{ )使用变量表达式输出:
{{ '{{' }}
对于较大的段落,标记一个块为 raw 是有意义的。例如展示 Jinja 语法的实例, 你可以在模板中用这个片段:
{% raw %} <ul> {% for item in seq %} <li>{{ item }}</li> {% endfor %} </ul> {% endraw %}
五、行语句¶
如果应用启用了行语句,就可以把一个行标记为一个语句。例如如果行语句前缀配置为 #,下面的两个例子是等价的:
<ul> # for item in seq <li>{{ item }}</li> # endfor </ul> <ul> {% for item in seq %} <li>{{ item }}</li> {% endfor %} </ul>
行语句前缀可以出现在一行的任意位置,只要它前面没有文本。为了语句有更好的可读 性,在块的开始(比如 for 、 if 、 elif 等等)以冒号结尾:
# for item in seq:
...
# endfor
提示:若有未闭合的圆括号、花括号或方括号,行语句可以跨越多行:
<ul> # for href, caption in [('index.html', 'Index'), ('about.html', 'About')]: <li><a href="{{ href }}">{{ caption }}</a></li> # endfor </ul>
如果配置 ## 为行注释前缀, 行中所有 ##之后的内容(不包括换行符)会被忽略:
# for item in seq: <li>{{ item }}</li> ## this comment is ignored # endfor
posted on 2015-08-03 16:51 myworldworld 阅读(1591) 评论(0) 收藏 举报
浙公网安备 33010602011771号