In Jinja2, how do you test if a variable is undefined?
From the Jinja2 template designer documentation: {% if variable is defined %} value of variable: {{ variable }} {% else %} variable is not defined {% endif %}
From the Jinja2 template designer documentation: {% if variable is defined %} value of variable: {{ variable }} {% else %} variable is not defined {% endif %}
{{ }} tells the template to print the value, this won’t work in expressions like you’re trying to do. Instead, use the {% set %} template tag and then assign the value the same way you would in normal python code. {% set testing = ‘it worked’ %} {% set another = testing %} {{ … Read more
<span>You have {{products|length}} products</span> You can also use this syntax in expressions like {% if products|length > 1 %} jinja2’s builtin filters are documented here; and specifically, as you’ve already found, length (and its synonym count) is documented to: Return the number of items of a sequence or mapping. So, again as you’ve found, {{products|count}} … Read more