These are a bunch of Jinja-related things I searched for once and might need again...
Stripping newline characters in a for loop
If you're iterating over a list and want to the output presented as:
ItemA ItemB ItemC ...
Instead of:
ItemA
ItemB
ItemC
...
You can do: {% for item in list -%} {{ item }} {%- endfor %}
Note the '-' in the for/endfor block.
Ensuring a list with one item is treated as a single-item list
Iterating over a list with exactly one string element in Jinja will generally lead to a situation where the element is parsed as i n d i v i d u a l c h a r a c t e r s. To avoid this, you can force the list to be interpreted as a list and specify the delimeter:
{% set list1 = my_list.split(',') %}
{% for item in list1 %}
{{ item }}
{% endfor %}
How to get elements from a dictionary of dictionaries
Sometimes your data is not stored conveniently in a simple list or dictionary. Sometimes the values in your dictionary are actually dictionaries themselves. In this case, you can use the dict.items() method:
{% for key, value in my_dictionary.items() %}
{{ value.sub_key }}
{% endfor %}
Other things...
More goes here.
No comments:
Post a Comment