Chinaunix首页 | 论坛 | 博客
  • 博客访问: 28710950
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Python/Ruby

2009-08-05 16:23:45

1.内建标签参考

block

定义一个能被子模板覆盖的区块。参见第四章“模板继承”一节查看更多信息。

comment

模板引擎会忽略掉 {% comment %}{% endcomment %} 之间的所有内容。

cycle

轮流使用标签给出的字符串列表中的值。

在一个循环内,轮流使用给定的字符串列表元素:

{% for o in some_list %}

...

{% endfor %}

在循环外,在你第一次调用时,给这些字符串值定义一个不重复的名字,以后每次只需使用这个名字就行了:

...
...
...

你可以使用任意数量的用逗号分隔的值。注意不要在值与值之间有空格,只是一个逗号。

debug

输出完整的调试信息,包括当前的上下文及导入的模块信息。

extends

标记当前模板扩展一个父模板。

这个标签有两种用法:

  • {% extends "base.html" %} (带引号) 直接使用要扩展的父模板的名字 "base.html"

  • {% extends variable %} 用变量 variable 的值来指定父模板。如果变量是一个字符串,Django会把字符串的值当作父模板的文件名。如果变量是一个 Template 对象,Django会把这个对象当作父模板。

参看第四章更多应用实例。

filter

通过可变过滤器过滤变量的内容。

过滤器也可以相互传输,它们也可以有参数,就像变量的语法一样。

看这个用法实例:

{% filter escape|lower %}
This text will be HTML-escaped, and will appear in all lowercase.
{% endfilter %}

firstof

输出传入的第一个不是 False 的变量,如果被传递变量都为 False ,则什么也不输出。

看这个用法实例:

{% firstof var1 var2 var3 %}

这等同于如下内容:

{% if var1 %}
{{ var1 }}
{% else %}{% if var2 %}
{{ var2 }}
{% else %}{% if var3 %}
{{ var3 }}
{% endif %}{% endif %}{% endif %}

for

轮询数组中的每一元素。例如显示一个指定的运动员的序列 athlete_list


    {% for athlete in athlete_list %}
  • {{ athlete.name }}

  • {% endfor %}

你也可以逆向遍历一个列表 {% for obj in list reversed %}

for 循环设置了许多循环中可用的变量(见表F-1)。

表F-1. {% for %}循环中的可用变量
变量名 描述
forloop.counter 当前循环次数(索引最小为1)。
forloop.counter0 当前循环次数 (索引最小为0)。
forloop.revcounter 剩余循环次数 (索引最小为1)。
forloop.revcounter0 剩余循环次数 (索引最小为0)。
forloop.first 第一次循环时为 True
forloop.last 最后一次循环时为 True
forloop.parentloop 用于嵌套循环,表示当前循环外层的循环。

8.if

{% if %} 标签测试一个变量,若变量为真(即其存在、非空,且不是一个为假的布尔值),区块中的内容就会被输出:

{% if athlete_list %}
Number of athletes: {{ athlete_list|length }}
{% else %}
No athletes.
{% endif %}

athlete_list 非空,变量 {{ athlete_list|length }} 就会显示运动员的数量。

正如你所见, if 标签有可选的 {% else %} 从句,若条件不成立则显示该从句。

if 语句可使用 andornot 来测试变量或者对给定的变量取反:


ifchanged

检查循环中一个值从最近一次重复其是否改变。

ifchanged 语句块用于循环中,其作用有两个:

它会把要渲染的内容与前一次作比较,发生变化时才显示它。例如,下面要显示一个日期列表,只有月份改变时才会显示它:

Archive for {{ year }}



{% for date in days %}
{% ifchanged %}

{{ date|date:"F" }}

{% endifchanged %}
{{ date|date:"j" }}
{% endfor %}

ifequal

如果两个参数相等,就输出该区块的内容。


ifnotequal

ifequal 类似,不过它是用来测试两个参数是 相等的。

include

加载一个模板,并用当前上下文对它进行渲染,这是在一个模板中包含其他模板的一种方法。

模板名可以是一个变量或者是一个硬编码(引号引起来的)的字符串,引号可以是单引号或者双引号。

这个例子包含了 "foo/bar.html" 模板的内容:

{% include "foo/bar.html" %}

load

读入一个自定义的模板库。(自定义标签加载进来)

spaceless

去除HTML标签之间的空白符号,包括制表符和换行符。


内建过滤器

Built-in Filter Reference


add

例如:

{{ value|add:"5" }}       表示这个值加5

Adds the argument to the value.


addslashes

例如:

{{ string|addslashes }}

Adds backslashes before single and double quotes. This is useful for passing strings to JavaScript, for example.

capfirst

例如:

{{ string|capfirst }}

将字符串的首字母大写

center

例如:

cut

例如:

{{ string|cut:"spam" }}

Removes all values of the argument from the given string.

date

Example:

{{ value|date:"F j, Y" }}

Formats a date according to the given format (same as the now tag).

dictsort

例如:

{{ list|dictsort:"foo" }}

divisibleby  (判断能否整除)

Example:

{% if value|divisibleby:"2" %}
Even!
{% else %}
Odd!
{% else %}

Returns True if the value is divisible by the argument.

escape

Example:

{{ string|escape }}

Escapes a strings HTML. Specifically, it makes these replacements:

  • "&" to "&"

  • < to "<"

  • > to ">"

  • '"' (double quote) to '"'

  • "'" (single quote) to '''


first

Example:

{{ list|first }}

Returns the first item in a list.

fix_ampersands

Example:

{{ string|fix_ampersands }}

floatformat

Examples:

{{ value|floatformat }} {{ value|floatformat:"2" }}

get_digit

Example:

{{ value|get_digit:"1" }}


join

例子:

{{ list|join:", " }}

Joins a list with a string, like Pythons str.join(list) .


striptags

Example:

{{ string|striptags }}







阅读(555) | 评论(0) | 转发(0) |
0

上一篇:Django配置参考

下一篇:二、八法则

给主人留下些什么吧!~~