如何在jinja2中打破for循环?

时间:2020-12-11 20:50:14

How can I break out of a for loop in jinja2?

如何在jinja2中跳出for循环?

my code is like this:

我的代码是这样的:

<a href="#">
{% for page in pages if page.tags['foo'] == bar %}
{{page.title}}
{% break %}
{% endfor %}
</a>

I have more than one page that has this condition and I want to end the loop, once the condition has been met.

我有不止一个页面有这个条件,我想结束循环,一旦条件满足。

3 个解决方案

#1


31  

You can't use break, you'd filter instead. From the Jinja2 documentation on {% for %}:

你不能用break,而是用过滤器。从Jinja2文件中{%为%}:

Unlike in Python it’s not possible to break or continue in a loop. You can however filter the sequence during iteration which allows you to skip items. The following example skips all the users which are hidden:

与Python不同,它不可能在循环中中断或继续。但是,您可以在迭代过程中过滤序列,这允许您跳过项目。下面的例子跳过了所有隐藏的用户:

{% for user in users if not user.hidden %}
    <li>{{ user.username|e }}</li>
{% endfor %}

In your case, however, you appear to only need the first element; just filter and pick the first:

但是,在您的例子中,您似乎只需要第一个元素;只需要过滤和选择第一个:

{{ (pages|selectattr('tags.foo', bar)|first).title }}

This filters the list using the selectattr() filter, the result of which is passed to the first filter.

这将使用selectattr()过滤器对列表进行筛选,其结果将传递给第一个过滤器。

The selectattr() filter produces an iterator, so using first here will only iterate over the input up to the first matching element, and no further.

selectattr()过滤器生成一个迭代器,因此使用first在这里只会遍历输入,直到第一个匹配元素,而不会进行进一步的迭代。

#2


6  

Break and Continue can be added to Jinja2 using the loop controls extension. Jinja Loop Control Just add the extension to the jinja environment.

可以使用循环控件扩展将Break和Continue添加到Jinja2。Jinja循环控件只是将扩展添加到Jinja环境中。

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

as per sb32134 comment

根据sb32134评论

#3


3  

But if you for some reason need a loop you can check the loop index inside for-loop block using "loop.first":

但是如果您出于某种原因需要循环,您可以使用“loop.first”检查for循环块中的循环索引:

{% for dict in list_of_dict %} 
    {% for key, value in dict.items() if loop.first %}
      <th>{{ key }}</th>
    {% endfor %} 
{% endfor %}

#1


31  

You can't use break, you'd filter instead. From the Jinja2 documentation on {% for %}:

你不能用break,而是用过滤器。从Jinja2文件中{%为%}:

Unlike in Python it’s not possible to break or continue in a loop. You can however filter the sequence during iteration which allows you to skip items. The following example skips all the users which are hidden:

与Python不同,它不可能在循环中中断或继续。但是,您可以在迭代过程中过滤序列,这允许您跳过项目。下面的例子跳过了所有隐藏的用户:

{% for user in users if not user.hidden %}
    <li>{{ user.username|e }}</li>
{% endfor %}

In your case, however, you appear to only need the first element; just filter and pick the first:

但是,在您的例子中,您似乎只需要第一个元素;只需要过滤和选择第一个:

{{ (pages|selectattr('tags.foo', bar)|first).title }}

This filters the list using the selectattr() filter, the result of which is passed to the first filter.

这将使用selectattr()过滤器对列表进行筛选,其结果将传递给第一个过滤器。

The selectattr() filter produces an iterator, so using first here will only iterate over the input up to the first matching element, and no further.

selectattr()过滤器生成一个迭代器,因此使用first在这里只会遍历输入,直到第一个匹配元素,而不会进行进一步的迭代。

#2


6  

Break and Continue can be added to Jinja2 using the loop controls extension. Jinja Loop Control Just add the extension to the jinja environment.

可以使用循环控件扩展将Break和Continue添加到Jinja2。Jinja循环控件只是将扩展添加到Jinja环境中。

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

as per sb32134 comment

根据sb32134评论

#3


3  

But if you for some reason need a loop you can check the loop index inside for-loop block using "loop.first":

但是如果您出于某种原因需要循环,您可以使用“loop.first”检查for循环块中的循环索引:

{% for dict in list_of_dict %} 
    {% for key, value in dict.items() if loop.first %}
      <th>{{ key }}</th>
    {% endfor %} 
{% endfor %}