django-template-3

标签

  • 标签的形式是: 标签要比变量复杂

  • 标签的作用

    在输出时创建一些文本
    通过执行循环和一些逻辑来实现控制流
    装载一些外部信息进入模板

  • 内建标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
autoescape  
使用形式:
{% autoescape off %}
(内容)
{% endautoescape %}
意义:当某块内容不需要自动转义的时候,这样写就可以了。当然如果块内某些地方需要转义的话,调用filter也可以。

block
使用形式:
{% block %}
(定义块内容)
{% endblock %}
意义:定义一个块,该块能够被继承他的子孙模板重写

comment
使用形式:
{% comment %}
(内容)
{% endcomment %}
意义:模板系统会忽略该标签内部的所有内容。

cycle
使用形式:
例如:
<tr class="{% cycle list%}">
...
</tr>
意义:在循环时轮流使用给定的字符串列表中的值。

extends
使用形式:{% extends "base.html" %}或者{% extends variable %}变量可以是一个字符串,也可以是一个模板对象。
意义:表示本模板要对指定的父模板进行扩展。

filter
使用形式:
{%filter force_escape|lower%}
(内容)
{%endfilter%}
意义:将filter 标签圈定的内容执行过滤器操作。

firstof
使用形式:{%firstof var1 var2 var3%}
意义:输出第一个值不等于False的变量
等价于:
{% if var1 %}
{{ var1 }}
{% else %}
{% if var2 %}
{{ var2 }}
{% else %}
{% if var3 %}
{{ var3 }}
{% endif %}
{% endif %}
{% endif %}

for
使用形式:
{% for variable in list/dict %}
(使用variable)
{% endfor%}
意义:循环list中的每个值,进行相应的输出
注意:
(a)也可以反向遍历{% for variable in list/dict reversed %}
(b)也可以{% for x, y in points %} points中的每个元素为 (x,y)
(c)也可以{% for key,value in data.items %} data是一个dictionary
for loop中定义的一些内建变量
forloop.counter 当前的迭代器数目(从1开始)
forloop.counter0 当前的迭代器数目(从0开始)
forloop.revcounter 当前的反向迭代器数目(从1开始)
forloop.revcounter0 当前的反向迭代器数目(从0开始)
forloop.first 值为True,如果是第一次通过迭代器
forloop.last 值为True,如果是最后一次通过迭代器
forloop.parentloop 对于嵌套循环,这是当前循环的上一层循环

for ... empty
使用形式如下:
{% for varibale in list %}
(内容1)
{% empty %}
(内容2)
{% endfor %}
意义:当list是空的时候,能够执行内容2,其形式等同于,先if判断list是否存在,然后在根据情况做什么操作。

if
使用形式如下 :
{% if variable %}
(内容1)
{% else %}
(内容2)
{% endif %}
注意:variable中可以使用and or 或者not,但是有一条必须记住,就是不允许and 和 or一起使用

ifchanged
使用形式:
(a)如果直接检测循环变量是否变化,那么使用:
{% ifchanged %}
(内容)
{% endifchanged %}
(b)如果检测循环变量的某个dot变量,例如循环变量是date,那么检测date.hour,那么使用:
{% ifchanged date.hour %}
(内容)
{% endifchanged %}
(c)ifchanged也可以加上一个{% else %}语句
意义:检测本次循环的值和上一次循环的值一样不一样,只能用在循环里面。

ifequal
使用形式:
{% ifequal variable1 variable2 %}
...
{% endifequal %}
意义:判断两个变量是否相等。

ifnotequal
使用与(12)相同

include
使用形式:{% include "foo/bar.html" %}或者{% include template_name %}
意义:将另外一个模板文件中的内容添加到该文件中。注意区别extend是继承。

now
使用形式:{% now "jS F Y H:i "%},注意存在需要转义的情况例如{% now "jS o\f F" %},因为f是格式化字符串
具体的格式化字符串如下所示


a 'a.m.' or 'p.m.' (Note that this is slightly different than PHP's output, because this includes periods to match Associated Press style.) 'a.m.'
A 'AM' or 'PM'. 'AM'
b Month, textual, 3 letters, lowercase. 'jan'
B Not implemented.
d Day of the month, 2 digits with leading zeros. '01' to '31'
D Day of the week, textual, 3 letters. 'Fri'
f Time, in 12-hour hours and minutes, with minutes left off if they're zero. Proprietary extension. '1', '1:30'
F Month, textual, long. 'January'
g Hour, 12-hour format without leading zeros. '1' to '12'
G Hour, 24-hour format without leading zeros. '0' to '23'
h Hour, 12-hour format. '01' to '12'
H Hour, 24-hour format. '00' to '23'
i Minutes. '00' to '59'
I Not implemented.
j Day of the month without leading zeros. '1' to '31'
l Day of the week, textual, long. 'Friday'
L Boolean for whether it's a leap year. True or False
m Month, 2 digits with leading zeros. '01' to '12'
M Month, textual, 3 letters. 'Jan'
n Month without leading zeros. '1' to '12'
N Month abbreviation in Associated Press style. Proprietary extension. 'Jan.', 'Feb.', 'March', 'May'
O Difference to Greenwich time in hours. '+0200'
P Time, in 12-hour hours, minutes and 'a.m.'/'p.m.', with minutes left off if they're zero and the special-case strings 'midnight' and 'noon' if appropriate. Proprietary extension. '1 a.m.', '1:30 p.m.', 'midnight', 'noon', '12:30 p.m.'
r RFC 2822 formatted date. 'Thu, 21 Dec 2000 16:01:07 +0200'
s Seconds, 2 digits with leading zeros. '00' to '59'
S English ordinal suffix for day of the month, 2 characters. 'st', 'nd', 'rd' or 'th'
t Number of days in the given month. 28 to 31
T Time zone of this machine. 'EST', 'MDT'
U Not implemented.
w Day of the week, digits without leading zeros. '0' (Sunday) to '6' (Saturday)
W ISO-8601 week number of year, with weeks starting on Monday. 1, 53
y Year, 2 digits. '99'
Y Year, 4 digits. '1999'
z Day of the year. 0 to 365
Z Time zone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.




spaceless
使用形式:{% spaceless %}
(内容)
{% endspaceless %}
意义:删除包围内容中的所有tab或者回车字符。

template
使用形式:{% templatetag %}
意义:模板系统本身没有转义的概念,因此如果要输出一个像“{%”这样的东东,就需要采用这种方式,否则就会语法错误
其参数有:
openblock {%
closeblock %}

openvariable {{
closevariable }}

openbrace {
closebrace }

opencomment {#
closecomment #}

with
使用形式:
{% with "expensive var1" as var2 %}
{% endwith %}
意义:当一个变量的访问消耗很大的模板解析时,可以用另外一个变量替换它,这种替换只有在with内部有效。

url
使用形式:{% url path.to.some_view arg1,arg2 %}
意义:给定某个module中函数的名字,给定参数,那么模板引擎给你一个URL,从而避免硬编码URL到代码中
注意:前提是URLconf中存在相应的映射,如果URLconf中没有该映射,那么会抛出异常,
这是可以选择使用
{% url path.to.view arg1 ,arg2 as the url %}
<a href="{{ the_url }}">Link to optional stuff</a>
其实这相当于
{% url path.to.view as the\_url %}
{% if the_url %}
<a href="{{ the_url }}">Link to optional stuff</a>
{% endif %}