以下是我的模板标记中的某种内容:
{% for filename, uuid, i in files %}
<div class="content">
{% if i == 0 %}
{% for filename, uuid, name in nginx %}
<span id="{{ uuid }}" class="{{ name }}">{{ filename }}</span>
<div id="{{ uuid }}" class="{{ name }}">
{% elif i == 2 %}
{% for filename, uuid, name in apache %}
<span id="{{ uuid }}" class="{{ name }}">{{ filename }}</span>
<div id="{{ uuid }}" class="{{ name }}">
{% end %}
那是来自服务器端:
class MainHandler(RequestHandler):
@asynchronous
def get(self, *args, **kwargs):
names = ['Nginx', 'Apache']
files = [(n, file_uuid(f), i) for n, f, i in zip(names, FILENAMES, range(len(FILENAMES)))]
nginx = [(op.basename(f), file_uuid(f), 'nginx') for f in NGINX]
httpd = [(op.basename(f), file_uuid(f), 'apache') for f in HTTPD]
kwargs = {
'hostname': self.request.host,
'files': files,
'nginx': nginx,
'apache': httpd,
}
self.render('template.html', **kwargs)
我的列表中有不同数量的文件,nginx和apache,我愿意将它们放在不同的标签上。
但龙卷风在这里说:
File "/usr/lib64/python2.7/site-packages/tornado/template.py", line 837, in _parse
block_body = _parse(reader, template, operator, operator)
File "/usr/lib64/python2.7/site-packages/tornado/template.py", line 789, in _parse
raise ParseError("%s block cannot be attached to %s block" % (operator, in_block))
ParseError: elif block cannot be attached to for block
根据Tornado docs, 继续/中断 如果/ elif / else 支持, 是否有针对龙卷风模板中的循环+ if语句支持? 如果没有,我以何种方式实现我的目的?
答案 0 :(得分:1)
在模板中,缩进并不重要,因此您必须在每个复合语句后使用{% end %}
标记:
{% for filename, uuid, i in files %}
<div class="content">
{% if i == 0 %}
{% for filename, uuid, name in nginx %}
<span id="{{ uuid }}" class="{{ name }}">{{ filename }}</span>
<div id="{{ uuid }}" class="{{ name }}">
{% end %}
{% elif i == 2 %}
{% for filename, uuid, name in apache %}
<span id="{{ uuid }}" class="{{ name }}">{{ filename }}</span>
<div id="{{ uuid }}" class="{{ name }}">
{% end %}
{% end %}
{% end %}
请注意,对于if/elif
,您只需要在end
之后elif
;不要在end
和if
之间放置elif
。