March 2019
Intermediate to advanced
412 pages
9h 49m
English
Loops can be combined with conditionals, as well. Within the loop structure, an if statement can be used to check a condition using the current loop item as part of the conditional. Let's extend our example and prevent the user of this template from accidentally using / as a data_dir (actions performed on the root directory of a filesystem can be dangerous, especially if they're performed recursively):
# data dirs
{% for dir in data_dirs -%}
{% if dir != "/" -%}
data_dir = {{ dir }}
{% endif -%}
{% else -%}
# no data dirs found
{% endfor -%}
The preceding example successfully filters out any data_dirs item that is /, but takes more typing than should be necessary. Jinja2 provides a convenience that allows you to filter ...
Read now
Unlock full access