Jinja joins the list of strings if the substring matches

Is it possible to do something like this in jinja2:

my_list = ['foo1', 'bar1', 'foo2', 'bar2'] # could be any number of foo and bar's
[i for i in my_list if 'foo' in i]

I looked at the map and join, something like:

{% my_list|map('???')|join(' ') %}

But I cannot find a filter that would like me to execute any wildcard queries. The two closest ones look like "equal" and "equal", but they do not work quietly.

+4
source share
1 answer

If you are using the latest version of Jinja2 (2.7 or later), there is a new filter called "select" that seems to do what you want. jinja.pocoo.org/docs/dev/templates/#select You may have to write your own test for this and pass it to the jinja2 object when you create it.

{{ foobars|select("test") }}

(2.8 ), http://jinja.pocoo.org/docs/dev/templates/#block-assignments

{% set my_foo_bars %}
    {%- for item in my_list %}
        {%- if item %}
 {{item}}
        {% endif -%}
    {% endfor -%}
{% endset %}

(, jinja2 Google), , , , , .

0

All Articles