Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use the native map function to map a list of tuples to one of its element in a Jinja template?

Let's say that I have a variable

t = [(1, 'hello'), (2, 'world')]

What I want is to map this to a new list containing only the second element of the tuples, in a Jinja template:

{% set s = t | map(...) | list %}

The variable s should be ['hello', 'world'].

Is this possible in some way without using a custom filter?

like image 621
heapOverflow Avatar asked Oct 19 '25 01:10

heapOverflow


1 Answers

Yep, it's the attribute method:

{% set t = [(1, 'hello'), (2, 'world')] %}
{% set t1 = t|map(attribute=1)|list %}
{{ t1 }}
like image 113
Sterling Paramore Avatar answered Oct 21 '25 14:10

Sterling Paramore