Karl Stulik
Karl Stulik

Reputation: 990

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'.

Upvotes: 2

Views: 9282

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121972

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 %}

Upvotes: 6

Related Questions