I have a Jinja template with a list of dictionaries. Order matters. I'd like to reduce the list or lookup values based on the keys/values of the dictionaries. Here's an example:
{%
set ordered_dicts = [
{
'id': 'foo',
'name': 'My name is Foo'
},
{
'id': 'bar',
'name': 'My name is Bar'
}
]
%}
If I have a variable some_id = 'foo', how do I get 'My name is Foo' out of ordered_dicts in my Jinja template?
I tried select() and selectattr() but couldn't figure them out based on the documentation. Here's what I tried:
{{ ordered_dicts|selectattr("id", "foo") }}
That outputs:
<generator object _select_or_reject at 0x10748d870>
I don't think I'm understanding the use of select() and selectattr() properly.
Do I need to iterate over the list and do the lookup manually?
Update:
As codegeek and gipi pointed out, I need to do something like this with the generator:
{{ ordered_dicts|selectattr("id", "foo")|list }}
The resulting error: TemplateRuntimeError: no test named 'foo', which clarifies how selectattr() works. The second argument has to be one of the builtin tests. As far as I can tell, none of these tests will let me check whether the value associated with a key matches another value. Here's what I'd like to do:
{{ ordered_dicts|selectattr("id", "sameas", "foo")|list }}
But that doesn't work, since the sameas test checks whether two objects are really the same object in memory, not whether two strings/numbers are equivalent.
So is it possible to pick an item based on a key/value comparison test?