3

I need to use break for a for loop in jinja2. Below is my code:

{% for i in range (0, desc) %}{{ desclist[i] }} {% set length = length + desclist[i]|length %}{% if length >= 70 %}{% break %}{% endif %}{% endfor %}

and to import the loopcontrol extension:

enter image description here

However, I got an error as followed:

jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'break'.
Jinja was looking for the following tags: 'elif' or 'else' or 'endif'. The
innermost block that needs to be closed is 'if'.

Any idea to make this work? Thank you.

benbenben
  • 159
  • 1
  • 3
  • 10

1 Answers1

0

Encountered the same issue, and realized on top of the document

it says:

The following example creates a Jinja environment with the i18n extension loaded: jinja_env = Environment(extensions=['jinja2.ext.i18n'])

So I tried this and it finally resolved the error of ... Encountered unknown tag ...:

jinja_env = Environment(extensions=['jinja2.ext.loopcontrols'])

scu
  • 1
  • 1