I have an array
{% set myarray = [['apple', true, false],
['banana', false, true]] %}
I want to get
['apple', 'banana']
why is something easy so hard with jinja?
You can use the do
extension for jinja2
The “do” aka expression-statement extension adds a simple do tag to the template engine that works like a variable expression but ignores the return value.
from flask import Flask
app = Flask(__name__)
app.jinja_env.add_extension('jinja2.ext.do')
{% set myarray = [['apple', true, false],['banana', false, true]] %}
{% set second_array = [] %}
{% for a in myarray %}
{% do second_array.append(a[0]) %}
{% endfor %}
{{ second_array }}
Another way you can do this, without using the do
extension, is to generate a custom filter
and use standard list comprehension
def get_nth(lst, i):
return [l[i] for l in lst]
app.jinja_env.filters['get_nth'] = get_nth
{{ myarray|get_nth(0) }}
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.