Tests allow you to return true/false values depending on the tested value.
If you use Visual Studio Code as your editor, you can use code snippets to speed up working with inserts.
You can find the snippets in our Github repository: https://github.com/Synerise/jinja-code-snippet
IsContainingAll
Returns true if a list contains all the values from another list.
Input:
| Type | Required | Description |
|---|---|---|
| list | yes | If this list contains all the items from the list provided in the params, the test returns true. |
Parameters:
| Type | Required | Description |
|---|---|---|
| list | yes | The list that must be contained in the list provided as input |
Example:
{{ [1, 2, 3] is containingall [2, 3] }}
IsContaining
Returns true if a list contains the provided value.
Input:
| Type | Required | Description |
|---|---|---|
| list | yes | If this list contains the value provided in the parameters, the test returns true. |
Parameters:
| Type | Required | Description |
|---|---|---|
| object | yes | The value to search for in the input list |
Example:
{{ [1, 2, 3] is containing 2 }}
IsDefined
Returns true if the variable is defined.
Input:
| Type | Required | Description |
|---|---|---|
| object | yes | The variable to check |
Example:
{%- if foo is defined -%}
{# code to render if foo is defined #}
{%- else -%}
{# code to render if foo is not defined #}
{%- endif -%}
IsDivisibleBy
Returns true if a variable is divisible by a number.
Input:
| Type | Required | Description |
|---|---|---|
| number | yes | The variable to check against the divisor |
Parameters:
| Type | Required | Description |
|---|---|---|
| number | yes | The divisor |
Example:
{%- if foo is divisibleby 5 -%}
{# code to render if foo can be divided by 5 #}
{%- else -%}
{# code to render if foo cannot be divided by 5 #}
{%- endif -%}
IsEqualTo
Returns true if an object has the same value as another object.
Input:
| Type | Required | Description |
|---|---|---|
| object | yes | First object for comparison |
Parameters:
| Type | Required | Description |
|---|---|---|
| object | yes | Second object for comparison |
Example:
{%- if foo is equalto 42 -%}
the foo attribute evaluates to the constant 42
{%- endif -%}
Usage with the selectattr filter:
{{ users|selectattr("email", "equalto", "foo@bar.invalid") }}
IsEven
Returns true if a value is an even number.
Input:
| Type | Required | Description |
|---|---|---|
| number | yes | The number to check |
Example:
{%- if foo is even -%}
{# code to render if foo is an even number #}
{%- else -%}
{# code to render if foo is an odd number #}
{%- endif -%}
IsIterable
Returns true if the object is iterable (for example, a sequence).
Input:
| Type | Required | Description |
|---|---|---|
| object | yes | The object to check |
Example:
{%- if foo is iterable -%}
{# code to render if foo is iterable #}
{%- endif -%}
IsLower
Returns true if a string is all lowercase.
Input:
| Type | Required | Description |
|---|---|---|
| string | yes | The string to check |
Example:
{%- if foo is lower -%}
{# code to render if the value of foo is all lowercase #}
{%- endif -%}
IsMapping
Returns true if an object is a dictionary.
Input:
| Type | Required | Description |
|---|---|---|
| object | yes | The object to check |
Example:
{%- if foo is mapping -%}
{# code to render if foo is a dictionary #}
{%- endif -%}
IsNumber
Returns true if the object is a number.
Input:
| Type | Required | Description |
|---|---|---|
| object | yes | The object to check |
Example:
{%- if foo is number -%}
{{ foo * 1000000 }}
{%- else -%}
foo is not a number.
{%- endif -%}
IsOdd
Returns true if a number is an odd number.
Input:
| Type | Required | Description |
|---|---|---|
| number | yes | The number to check |
Example:
{%- if foo is odd -%}
{# code to render if foo is an odd number #}
{%- else -%}
{# code to render if foo is an even number #}
{%- endif -%}
IsSameAs
Returns true if a variable points at same object as another variable.
Input:
| Type | Required | Description |
|---|---|---|
| object | yes | The first variable to compare |
Parameters:
| Type | Required | Description |
|---|---|---|
| object | yes | The second variable to compare |
Example:
{%- if var_one is sameas var_two -%}
{# code to render if the variables have identical values #}
{%- endif -%}
IsSequence
Returns true if the variable is a sequence. Sequences are variables that are iterable.
Input:
| Type | Required | Description |
|---|---|---|
| object | yes | The object to check |
Example:
{%- if foo is sequence -%}
{# code to render foo is a sequence #}
{%- endif -%}
IsStringContaining
Returns true if an object is a string which contains a specified other string.
Input:
| Type | Required | Description |
|---|---|---|
| string | yes | The string that needs to contain the string provided as the parameter |
Parameters:
| Type | Required | Description |
|---|---|---|
| string | yes | The string that needs to be included in the string provided as input |
Example:
{%- if foo is string_containing 'bar' -%}
{# code to render if foo contains 'bar' #}
{%- endif -%}
IsString
Returns true if an object is a string.
Input:
| Type | Required | Description |
|---|---|---|
| object | yes | The object to check |
Example:
{%- if foo is string -%}
{# code to render if foo is a string #}
{%- endif -%}
IsStringStartingWith
Returns true if an object is a string which starts with a specified other string.
Input:
| Type | Required | Description |
|---|---|---|
| string | yes | The string that needs to start with the string provided as the parameter |
Parameters:
| Type | Required | Description |
|---|---|---|
| string | yes | The string to check against |
Example:
{%- if foo is string_startingwith 'bar' -%}
{# code to render if foo starts with 'bar' #}
{%- endif -%}
IsTruthy
Returns true if a value is truthy. IsTruthy follows the same implicit conversion rules as {% if %} conditions. For == and !=, type conversion applies only when the other operand is a boolean.
| Input type | Returns true |
Returns false |
|---|---|---|
| Boolean | true |
false |
| Number | Any non-zero number | 0 |
| String | Any non-empty string, except "false" |
"" (empty string) or "false" |
| null | Never | Always |
| Collection (list, map) | Non-empty | Empty |
| Any other type | Always | Never |
The |bool filter uses stricter conversion rules and can return a different result from is truthy for the same input. For example, 'string' is truthy returns true, but 'string'|bool returns false.
Input:
| Type | Required | Description |
|---|---|---|
| value | yes | The value to check |
Example:
{%- if foo is truthy -%}
{# code to render if foo is truthy #}
{%- endif -%}
For strict type-and-value equality without any boolean conversion, use the sameas test. Unlike is truthy, sameas checks that two values are identical without type coercion. For example, "1" == true returns true due to boolean conversion, but "1" is sameas true returns false because the types differ.
IsUndefined
Returns true if an object is undefined.
Input:
| Type | Required | Description |
|---|---|---|
| object | yes | The object to check |
Example:
{%- if foo is undefined -%}
{# code to render if foo is undefined #}
{%- endif -%}
IsUpper
Returns true if a string is all uppercase.
Input:
| Type | Required | Description |
|---|---|---|
| string | yes | The string to check |
Example:
{%- if foo is upper -%}
{# code to render if foo is a string and is all uppercase #}
{%- endif -%}
IsWithin
Returns true if a value is contained in a list.
Input:
| Type | Required | Description |
|---|---|---|
| object | yes | The value to search for in the list |
Parameters:
| Type | Required | Description |
|---|---|---|
| list | yes | The list to search in |
Example:
{{ 2 is within [1, 2, 3] }}