笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册

模板设计者文档

发布者: 笨鸟自学网



逻辑

对于 if 语句,在 for 过滤或 if 表达式中,它可以用于联合多个表达式:

and
如果左操作数和右操作数同为真,返回 true 。
or
如果左操作数和右操作数有一个为真,返回 true 。
not
对一个表达式取反(见下)。
(expr)
表达式组。

提示

is 和 in 运算符同样支持使用中缀记法: foo is not bar 和 foo not in bar 而不是 not foo is bar 和 not foo in bar 。所有的 其它表达式需要前缀记法 not (foo and bar) 。

其它运算符

下面的运算符非常有用,但不适用于其它的两个分类:

in
运行序列/映射包含检查。如果左操作数包含于右操作数,返回 true 。比如 {{ 1 in [1,2,3] }} 会返回 true 。
is
运行一个 测试 。
|
应用一个 过滤器 。
~
把所有的操作数转换为字符串,并且连接它们。 {{ "Hello " ~ name ~ "!" }} 会返回(假设 name 值为 ''John' ) Hello John! 。
()
调用一个可调用量:{{ post.render() }} 。在圆括号中,你可以像在 python 中一样使用位置参数和关键字参数: {{ post.render(user, full=true) }} 。
. / []
获取一个对象的属性。(见 变量 )

If 表达式

同样,也可以使用内联的 if 表达式。这在某些情况很有用。例如你可以用来在一个 变量定义的情况下才继承一个模板,否则继承默认的布局模板:

{% extends layout_template if layout_template is defined else 'master.html' %}

一般的语法是 <do something> if <something is true> else <do something else> 。

else 部分是可选的。如果没有显式地提供 else 块,会求值一个未定义对象:

{{ '[%s]' % page.title if page.title }}

内置过滤器清单

abs(number)

Return the absolute value of the argument.

attr(objname)

Get an attribute of an object. foo|attr("bar") works like foo.bar just that always an attribute is returned and items are not looked up.

See Notes on subscriptions for more details.

batch(valuelinecountfill_with=None)

A filter that batches items. It works pretty much like slice just the other way round. It returns a list of lists with the given number of items. If you provide a second parameter this is used to fill up missing items. See this example:

<table>
{%- for row in items|batch(3, '&nbsp;') %}
  <tr>
  {%- for column in row %}
    <td>{{ column }}</td>
  {%- endfor %}
  </tr>
{%- endfor %}
</table>
capitalize(s)

Capitalize a value. The first character will be uppercase, all others lowercase.

center(valuewidth=80)

Centers the value in a field of a given width.

default(valuedefault_value=u''boolean=False)

If the value is undefined it will return the passed default value, otherwise the value of the variable:

{{ my_variable|default('my_variable is not defined') }}

This will output the value of my_variable if the variable was defined, otherwise 'my_variable is not defined'. If you want to use default with variables that evaluate to false you have to set the second parameter to true:

{{ ''|default('the string was empty', true) }}
Aliases:d

上一篇:沙箱下一篇:扩展

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-9-17 04:30 , Processed in 0.110361 second(s), 17 queries .

© 2001-2020

返回顶部