[Dynamic Language] Python Django: 模板引擎(3)模板标签和过滤器

模板系统带有内置的标签和过滤器

标签

if/else

代码
{% if name %}
<p
>Welcome name</p>
{
% endif %}
-------------------------------------------
{
% if today_is_weekend %}
<p
>Welcome to the weekend!</p>
{
% else %}
<p
>Get back to work.</p>
{
% endif %}


在Python和Django模板系统中,以下对象相当于布尔值的False
空列表([])、空元组(())、空字典({})、空字符串('')、零值(0)、特殊对象None、对象False.

代码
{% if athlete_list and coach_list %}
Both athletes and coaches are available.
{% endif %}

{% if not athlete_list %}
There are no athletes.
{% endif %}

{% if athlete_list or coach_list %}
There are some athletes or some coaches.
{% endif %}

{% if not athlete_list or coach_list %}
There are no athletes or there are some coaches.
{% endif %}

{% if athlete_list and not coach_list %}
There are some athletes and absolutely no coaches.
{% endif %}

没有 {% elif %} 标签, 我们只能使用嵌套的`` {% if %}`` 标签来达成同样的效果:

{% if athlete_list %}
<p>Here are the athletes: {{ athlete_list }}.</p>
{% else %}
<p>No athletes are available.</p>
{% if coach_list %}
<p>Here are the coaches: {{ coach_list }}.</p>
{% endif %}
{% endif %}

一定要用 {% endif %} 关闭每一个 {% if %} 标签。

系统不支持用圆括号来组合比较操作。 如果需要用到圆括号来组合表达你的逻辑式,考虑将它移到模板之外处理,然后以模板变量的形式传入结果吧。
或者,仅仅用嵌套的{% if  %}标签替换吧,就像这样:

{% if athlete_list %}
{
% if coach_list or cheerleader_list %}
We have athletes, and either coaches or cheerleaders
!
{
% endif %}
{
% endif %}

 

for循环标签

代码
10 <ul>
11 {% if namelist %} // 列表为空时,输出列表为空信息
12 {% for name in namelist %}
13
<li> {{ name }} </li>
14 {% for subname in name %}
15
<li>{{ subname }}</li>
16 {% endfor %}
17 {% endfor %}
18 {% else %}
19 the namelist is empty.
20 {% endif %}
21
</ul>

 

 for标签支持一个可选的{% empty %}分支,通过它可以定义列表为空时的输出内容,看下面是否更简洁呢!

代码
23 <ul>
24 {% for name in namelist %}
25
<li> {{ name }} </li>
26 {% for subname in name %}
27
<li>{{ subname }}</li>
28 {% endfor %}
29 {% empty %}
30 the namelist is empty.
31 {% endfor %}
32
</ul>

 
在每个`` {% for %}``循环里有一个称为`` forloop`` 的模板变量。这个变量有一些提示循环进度信息的属性。
forloop.counter

  表示当前循环的执行次数的整数计数器。这个计数器是从1开始的,所以在第一次循环时 forloop.counter 将会被设置为1。
forloop.counter0

  类似于 forloop.counter ,但是它是从0计数的。 第一次执行循环时这个变量会被设置为0。
forloop.revcounter 

  表示循环中剩余项的整型变量。 在循环初次执行时 forloop.revcounter  将被设置为序列中项的总数。 最后一次循环执行中,这个变量将被置1。
forloop.revcounter0

   类似于 forloop.revcounter ,但它以0做为结束索引。在第一次执行循环时,该变量会被置为序列的项的个数减1。
forloop.first

  是一个布尔值,如果该迭代是第一次执行,那么它被置为````。
forloop.last 

  是一个布尔值;在最后一次执行循环时被置为True。 一个常见的用法是在一系列的链接之间放置管道符(|)
forloop.parentloop

    是一个指向当前循环的上一级循环的 forloop  对象的引用(在嵌套循环的情况下)。 


forloop  变量仅仅能够在循环中使用。 在模板解析器碰到{%  endfor %}标签后,forloop就不可访问了。

代码
23 <ul>
24 {% for name in namelist %}
25 {% if forloop.first %}
26 this first name is : {{ name }}
</br>
27 {% endif %}
28 {{ forloop.counter }} : {{ name }}
29
30 {% for subname in name %}
31
<li>{{ subname }}{% if not forloop.last %},{% endif %}</li>
32 {% endfor %}
33
34 {% if forloop.last %}
35 this last name is : {{ name }}
</br>
36 {% endif %}
37 {% empty %}
38 the namelist is empty.
39 {% endfor %}
40
</ul>

 

ifequal/ifnotequal

{%  ifequal %} 标签比较两个值,当他们相等时,显示在 {%  ifequal %} 和 {% endifequal  %} 之中所有的值。
只有模板变量,字符串,整数和小数可以作为 {% ifequal %} 标签的参数。

4 {% ifequal name name1 %}
5
<p> welcome {{ name }} and {{ name1 }}</p>
6 {% else %}
7
<p> the name != name1 </p>
8 {% endifequal %}


注释
单行注释使用
{# 注释 #}  

多行注释

{% comment %}
注释
注释
注释
{% endcomment %}

 


过滤器
模板过滤器是在变量被显示前修改它的值的一个简单方法。
过滤器使用管道字符,可以被* 套接* ,既是说,一个过滤器管道的输出又可以作为下一个管道的输入,如此下去.

<p> welcome {{ name|upper }} and {{ name1|lower|upper }}</p>
 

几个重要的过滤器
addslashes : 添加反斜杠到任何反斜杠、单引号或者双引号前面。 这在处理包含JavaScript的文本时是非常有用的。
date : 按指定的格式字符串参数格式化 date 或者 datetime 对象, 范例:
{{ pub_date|date:"F j, Y" }}

length : 返回变量的长度。 对于列表,这个参数将返回列表元素的个数。 对于字符串,这个参数将返回字符串中字符的个数。 你可以对列表或者字符串,或者任何知道怎么测定长度的Python 对象使用这个方法(也就是说,有 __len__() 方法的对象)。
 
更多信息参考
附录F 内建的模板标签和过滤器

posted @ 2010-06-20 23:07  ABeen  阅读(631)  评论(0编辑  收藏  举报