I am new to jinja and need some help with doing the following:
main.tpl has the following content:
{%- with PREFIX = "test." %}
{%- set TEST_VARIABLE = "false" %}
{%- include "child1.tpl" %}
{%- endwith %}
How can I use TEST_VARIABLE in child1.tpl such that when it is false, I set a property like this to false else true:
{%- if {TEST_VARIABLE} == false %}
PROPERTY1=false
{%- else %}
PROPERTY1=true
{%- endif %}
I get and error saying unexpected '{' in field name.
There are two errors in your child1.tpl
file. First, you are using invalid syntax to refer to a variable in your if
statement. Second, you are comparing a string ("false"
) with a boolean variable (false
). This won't do what you want. Fixing both of these issues gives you:
{%- if TEST_VARIABLE == "false" %}
PROPERTY1=false
{%- else %}
PROPERTY1=true
{%- endif %}
Using this and your main.tpl
works without errors. Changing set TEST_VARIABLE = "false"
to set TEST_VARIABLE = "something else"
changes the final template output as expected.
With your original main.tpl
and child1.tpl
as above:
$ jinja2 main.tpl
PROPERTY1=false
If I modify main.tpl
to look like:
{%- with PREFIX = "test." %}
{%- set TEST_VARIABLE = "something else" %}
{%- include "child1.tpl" %}
{%- endwith %}
Then I get:
$ jinja2 main.tpl
PROPERTY1=true