@nalan90
2018-06-28T18:10:09.000000Z
字数 3000
阅读 710
Python高效编程技巧实战
常用标签
There are a few kinds of delimiters. The default Jinja delimiters are configured as follows
{% ... %} for Statements
{{ ... }} for Expressions to print to the template output
{# ... #} for Comments not included in the template output
# ... ## for Line Statements
For
## Loop over each item in a sequence. For example, to display a list of users provided in a variable called users:
<h1>Members</h1>
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
--------------------
## As variables in templates retain their object properties, it is possible to iterate over containers like dict:
<dl>
{% for key, value in my_dict.iteritems() %}
<dt>{{ key|e }}</dt>
<dd>{{ value|e }}</dd>
{% endfor %}
</dl>
{% for row in rows %}
<li class="{{ loop.cycle('odd', 'even') }}">{{ row }}</li>
{% endfor %}
{% for user in users if not user.hidden %}
<li>{{ user.username|e }}</li>
{% endfor %}
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% else %}
<li><em>no users found</em></li>
{% endfor %}
</ul>
<ul class="sitemap">
{%- for item in sitemap recursive %}
<li><a href="{{ item.href|e }}">{{ item.title }}</a>
{%- if item.children -%}
<ul class="submenu">{{ loop(item.children) }}</ul>
{%- endif %}</li>
{%- endfor %}
</ul>
If
{% if users %}
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
{% endif %}
## For multiple branches, elif and else can be used like in Python. You can use more complex Expressions there, too:
{% if kenny.sick %}
Kenny is sick.
{% elif kenny.dead %}
You killed Kenny! You bastard!!!
{% else %}
Kenny looks okay --- so far
{% endif %}
Assignments
{% set iterated = false %}
{% for item in seq %}
{{ item }}
{% set iterated = true %}
{% endfor %}
{% if not iterated %} did not iterate {% endif %}
It is not possible with Jinja syntax to do this. Instead use alternative constructs like the loop else block or the special loop variable:
{% for item in seq %}
{{ item }}
{% else %}
did not iterate
{% endfor %}
示例代码
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from jinja2 import Template
def render_str(str, data):
template = Template(str)
val = template.render(data)
return val
with open('tpl01','r') as f:
data = {
'name' : 'zhangshuang'
}
content = f.read()
print render_str(content, data)
## tpl01
My name is {{ name }}
## 运行结果
(/anaconda) work:jinja2 ys$ python demo01.py
My name is zhangshuang
----------------------------------------
with open('tpl02','r') as f:
data = {
'user' : {
'name': 'zhangshuang'
},
'age' : 27
}
content = f.read()
print render_str(content, data)
## tpl02
name : {{ user.name }}
age : {{ age }}
## 运行结果
name : zhangshuang
age : 27
----------------------------------------
with open('tpl03','r') as f:
data = {
'user_num_start' : 1000,
'user_list' : [
{
'name' : 'zhangshuang',
'age' : 27
},
{
'name' : 'zhangsan',
'age' : 28
},
{
'name' : 'lisi',
'age' : 30
},
{
'name': 'unknow',
'age': 30
}
]
}
content = f.read()
print render_str(content, data)
## tpl03
{%- for item in user_list %}
<li>name : {{ item.name }},age : {{ item.age }}</li>
{%- endfor %}
{%- for item in user_list -%}
{% set user_num = loop.index + user_num_start %}
{%- if item.name != 'unknow' %}
<li>name : {{ item.name }}, age : {{ item.age }}, user_num : {{ user_num }}</li>
{%- endif %}
{%- endfor -%}
## 运行结果
<li>name : zhangshuang,age : 27</li>
<li>name : zhangsan,age : 28</li>
<li>name : lisi,age : 30</li>
<li>name : unknow,age : 30</li>
<li>name : zhangshuang, age : 27, user_num : 1001</li>
<li>name : zhangsan, age : 28, user_num : 1002</li>
<li>name : lisi, age : 30, user_num : 1003</li>