Skip to content
Advertisement

How to evaluate a variable within a dict in jinja

I would like to evaluate a variable within a dict object in Jinja. Is that possible?

{%- set obj_list = ['obj1', 'obj2'] %}
{%- set long_dict_set ={"key":"text text {{ obj_list }} text text"} %}

{{ long_dict_set }}

--Returns
>> {'key': 'text text {{ obj_list }} text text'}

In the example, {{ obj_list }} is treated as text. Is there a syntax to evaluate this variable within?

Advertisement

Answer

You can use ~ to include variables within strings:

{%- set long_dict_set = {"key":"text text " ~ obj_list ~ " text text"} %}

Output:

{'key': "text text ['obj1', 'obj2'] text text"}

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement