Conditionals: if / else / elif

The if statement in Jinja is comparable with the Python if statement. In the simplest form, you can use it to check whether a condition is true, like whether two numbers are equal or bigger/smaller:

{% if number == 1 %}
	Number is 1.
{% elif number < 10 %}
	Number is smaller than 10.
{% else %}
	Number is bigger than 10.
{% endif %}

To use conditionals you can use comparisons, logical operators and tests.

In the absence of any of such operators, the value in question will be evaluated an return either true of false depending on its truthiness.

Comparisons

To make use of conditionals your first need to know how to compare variables or values.
To do so, you have to following operators available in Jinja:

  • ==: equal to
  • !=: not equal to
  • >: greater than
  • <: less than
  • >=: greater than or equal to
  • <=: less than or equal to
{% if variable == 'foo' %}
	{{ variable }}
{% else %}
	Variable is not equal to 'foo'.
{% endif %}

It's also important to note that when using comparison operator like == or != in Jinja, you need to be careful, as they behave differently than they might in other languages. When using comparison operator to check object's equality, jinja is comparing their identity not their value.

Logical Operators

No implementation of conditionals would be complete without logical operators.

In Jinja, you can use the following logical operators to combine comparison expressions:

  • and: returns True if both comparison expressions are True
  • or: returns True if either comparison expression is True
  • not: returns True if the comparison expression is False


{% if number > 1 and number < 10 %}
	{{ number }}
{% else %}
	Number is not in between 1 and 10.
{% endif %}

Truthiness

In Jinja, certain values are considered "truthy" and others are considered "falsy". When an expression is used in a conditional statement (such as an if or elif block), the value of the expression is first evaluated and then treated as either True or False based on its "truthiness".

In Jinja, the following values are considered "falsy":

  • None
  • False
  • 0 (integer)
  • 0.0 (float)
  • empty strings (e.g. '')
  • empty lists (e.g. [])
  • empty dicts (e.g. {})
  • empty sets (e.g. set())

All other values are considered "truthy". This includes, for example, any non-empty string or non-zero number, as well as any non-empty data structure such as a list with items or a dictionary with key-value pairs.

Here's an example:

{% if variable %} 
	 Variable is truthy
 {% else %}
     Variable is falsy
 {% endif %}

Tests

Tests in Jinja2 are used to evaluate variables and determine if they pass a certain condition. They return a boolean value of either True or False, based on the outcome of the test.

To use this feature, simply add the is keyword followed by the test name after the variable.

The most commonly used tests is the "defined" test, which checks if a specific variable is present and defined in the data that is currently avaible to the JINJA intepreter.
This is particularly useful in ensuring that your Jinja Query handles undefined variables properly and can prevent incomplete outputs from being generated.

{% if variable is defined %} 
	 Variable is defined
 {% else %}
     Variable is undefined
 {% endif %}

Another useful family of tests are those used to check the data type of a variable. Certain operations, such as comparing numbers or iterating over lists and dictionaries, require that variables are of the same data type.

These tests can help you catch type mismatches and prevent errors from being thrown by Jinja.

boolean - check is variable is a boolean
integer - check if variable is an integer
float - check if variable is a float
number - check if variable is number, will return True for both integer and float
string - check if variable is a string
mapping - check if variable is a mapping, i.e. dictionary
iterable - check if variable can be iterated over, will match string, list, dict, etc.
sequence - check if variable is a sequence

An exmaple :

{% if variable is iterable %} 
	 {% for item in variable %}
     	{{ item }}
     {% endfor %}
 {% else %} 
 	{{ variable }}
 {% endif %}

The above test checks whether the defined variable is iterable, and if so, iterates over it returning all contained items.
Else, it simply returns the variable.


Jump to

Start shipping smarter reports today

Sign up and start using Query.me for free.
Or schedule a demo and discuss your use case.