If语句on for语句in Jinja2

2024-03-28 12:52:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我在GAE中有一个网站,在Jinja2模板中有一个FOR语句中的IF语句。

我使用以下方法启用了jinja2.ext.loopcontrols循环控制:

template_dir = os.path.dirname(__file__)
ENV = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir),
                                       autoescape=True,
                                       extensions=['jinja2.ext.autoescape',
                                                   'jinja2.ext.loopcontrols'])

我的模板如下:

{% for i in data %}
  (% if i.date_posted is defined %)
    {{ i.date_posted.strftime('%d %b %Y') }}
  {% else %}
    No
  {% endif %}
{% endfor %}

我不断地发现错误:

TemplateSyntaxError: Encountered unknown tag 'endif'. Jinja was looking for the following tags: 'endfor'. The innermost block that needs to be closed is 'for'.


Tags: 模板jinja2fordateisdirtemplate语句