Jinja2 template syntax
From wikinotes
Jinja has some neat tricks up it's sleeve for various datatypes.
Block Whitespace
By default every
{%...%}
gets a newline, which makes configfiles rendered by jinja very ugly.
You can avoid this by adding-%}
for your closing block.{% if foo in bar -%} Key = Value {% endif -%}
Dictionaries
dictionaries can be accessed using periods instead of
[]
{% set foo = {'a': {'b': None}} %} {{ foo.a.b }}You can verify the existence of a nested dictionary without KeyError using
is defined
.
(this works for both dot-dict syntax and traditional dict syntax){% set foo = {'a': {'b': None}} %} {% if foo.a.b.c.d is defined %} # this will not be rendered {% endfor %}Apparently you can also use the
default
filter to assign values where undefined, but it hasn't worked for me.