简体   繁体   中英

If statement on for statement in Jinja2

I have a website hosted in GAE and within my Jinja2 template I have an IF statement in a FOR statement.

I have enabled my jinja2.ext.loopcontrols loop control using:

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

My template looks like this:

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

I keep getting the error:

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

You didn't declare your if tag properly:

(% if i.date_posted is defined %)

Note the parentheses instead of curly braces; it should be written like:

{% if i.date_posted is defined %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM