您的位置:首页 > 其它

Djagno入门:template之标签

2016-07-28 18:41 489 查看
【转载】

三、标签

1.标签的形式是:{% tag %},标签要比变量复杂

2.标签的作用

(1)在输出时创建一些文本

(2)通过执行循环和一些逻辑来实现控制流

(3)装载一些外部信息进入模板

3.内建标签

(1)autoescape

使用形式:

{% autoescape off %}

(内容)

{% endautoescape %}

意义:当某块内容不需要自动转义的时候,这样写就可以了。当然如果块内某些地方需要转义的话,调用filter也可以。

(2)block

使用形式:

{% block %}

(定义块内容)

{% endblock %}

意义:定义一个块,该块能够被继承他的子孙模板重写

(3)comment

使用形式:

{% comment %}

(内容)

{% endcomment %}

意义:模板系统会忽略该标签内部的所有内容。

(4)cycle

使用形式:

例如:

<\tr class=”{% cycle list%}”>



<\/tr>

意义:在循环时轮流使用给定的字符串列表中的值。

(5)extends

使用形式:{% extends “base.html” %}或者{% extends variable %}变量可以是一个字符串,也可以是一个模板对象。

意义:表示本模板要对指定的父模板进行扩展。

(6)filter

使用形式:

{%filter force_escape|lower%}

(内容)

{%endfilter%}

意义:将filter 标签圈定的内容执行过滤器操作。

(7)firstof

使用形式:{%firstof var1 var2 var3%}

意义:输出第一个值不等于False的变量

等价于:

{% if var1 %}

{{ var1 }}

{% else %}

{% if var2 %}

{{ var2 }}

{% else %}

{% if var3 %}

{{ var3 }}

{% endif %}

{% endif %}

{% endif %}

(8)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 对于嵌套循环,这是当前循环的上一层循环

(9)for … empty

使用形式如下:

{% for varibale in list %}

(内容1)

{% empty %}

(内容2)

{% endfor %}

意义:当list是空的时候,能够执行内容2,其形式等同于,先if判断list是否存在,然后在根据情况做什么操作。

(10)if

使用形式如下 :

{% if variable %}

(内容1)

{% else %}

(内容2)

{% endif %}

注意:variable中可以使用and or 或者not,但是有一条必须记住,就是不允许and 和 or一起使用

(11)ifchanged

使用形式:

(a)如果直接检测循环变量是否变化,那么使用:

{% ifchanged %}

(内容)

{% endifchanged %}

(b)如果检测循环变量的某个dot变量,例如循环变量是date,那么检测date.hour,那么使用:

{% ifchanged date.hour%}

(内容)

{% endifchanged %}

(c)ifchanged也可以加上一个{% else %}语句

意义:检测本次循环的值和上一次循环的值一样不一样,只能用在循环里面。

(12)ifequal

使用形式:

{% ifequal variable1 variable2 %}



{% endifequal %}

意义:判断两个变量是否相等。

(13)ifnotequal

使用与(12)相同

(14)include

使用形式:{% include “foo/bar.html” %}或者{% include template_name %}

意义:将另外一个模板文件中的内容添加到该文件中。注意区别extend是继承。

(15)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.

(16)spaceless

使用形式:{% spaceless %}

(内容)

{% endspaceless %}

意义:删除包围内容中的所有tab或者回车字符。

(17)template

使用形式:{% templatetag %}

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

其参数有:

openblock {%

closeblock %}

openvariable {{

closevariable }}

openbrace {

closebrace }

opencomment {#

closecomment #}

(18)with

使用形式:

{% with “expensive var1” as var2 %}

{% endwith %}

意义:当一个变量的访问消耗很大的模板解析时,可以用另外一个变量替换它,这种替换只有在with内部有效。

(19)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 %}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  template 标签