Operators are used to perform logical operations within templated expressions such as comparing values and performing arithmetic operations.
Comparison operators
Pebble provides the following comparison operators: ==
, !=
, <
, >
, <=
, >=
. All of them except for ==
are equivalent to their Java counterparts. The ==
operator uses Java.util.Objects.equals(a, b)
behind the
scenes to perform null safe value comparisons.
equals
is an alias for==
{% if user.name equals "Mitchell" %}
...
{% endif %}
concat
The concat
operator can be used to concatenate 2 strings:
{{ "apple" ~ "pear" ~ "banana" }}
{# results in: 'applepearbanana' #}
contains
The contains
operator can be used to determine if a collection, map, or array contains a particular item.
{% if ["apple", "pear", "banana"] contains "apple" %}
...
{% endif %}
When using maps, the contains operator checks for an existing key.
{% if {"apple":"red", "banana":"yellow"} contains "banana" %}
...
{% endif %}
The operator can be used to look for multiple items at once:
{% if ["apple", "pear", "banana", "peach"] contains ["apple", "peach"] %}
...
{% endif %}
is
The is
operator will apply a test to a variable which will return a boolean.
{% if 2 is even %}
...
{% endif %}
The result can be negated using the not
operator:
{% if 3 is not even %}
...
{% endif %}
logic
The and
operator and the or
operator are available to join boolean expressions.
{% if 2 is even and 3 is odd %}
...
{% endif %}
The not
operator is available to negate a boolean expression.
{% if 3 is not even %}
...
{% endif %}
Parenthesis can be used to group expressions to ensure a desired precedence.
{% if (3 is not even) and (2 is odd or 3 is even) %}
...
{% endif %}
math
All the regular math operators are available for use. Order of operations applies.
{{ 2 + 2 / ( 10 % 3 ) * (8 - 1) }}
The following operators are supported:
+
: Adds two numbers together (the operands are cast to numbers).{{ 1 + 1 }}
is 2.-
: Subtracts the second number from the first one.{{ 3 - 2 }}
is 1./
: Divides two numbers. The returned value will be a floating point number.{{ 1 / 2 }}
is{{ 0.5 }}
.%
: Calculates the remainder of an integer division.{{ 11 % 7 }}
is 4.*
: Multiplies the left operand with the right one.{{ 2 * 2 }}
would return 4.
The result can be negated using the not operator.
not
The not
operator is used in conjunction with is will negate the test.
{% if 3 is not even %}
...
{% endif %}
null-coalescing
Kestra supports the null-coalescing operator that allows testing if variables are defined.
The ??
operator will return the first defined, not-null value in the list.
The ???
operator will return the right-hand side of the expression only if the left-hand side is undefined.
TL;DR for the null-coalescing operator:
X ?? Y
will returnY
ifX
is null or undefinedX ??? Y
will returnY
only ifX
is undefined
{% set baz = "baz" %}
{{ foo ?? bar ?? baz }}
{# results in: 'baz' #}
{{ foo ?? bar ?? raise }}
{# results: an exception because none of the 3 vars is defined #}
For more details on using the null-coalescing operator, see the Handling null and undefined values guide.
ternary-operator
Pebble supports the use of the conditional operator (often named the ternary operator).
{{ foo == null ? bar : baz }}
Was this page helpful?