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 ==

twig
{% if user.name equals "Mitchell" %}
    ...
{% endif %}

concat

The concat operator can be used to concatenate 2 strings:

twig
{{ "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.

twig
{% if ["apple", "pear", "banana"] contains "apple" %}
    ...
{% endif %}

When using maps, the contains operator checks for an existing key.

twig
{% if {"apple":"red", "banana":"yellow"} contains "banana" %}
    ...
{% endif %}

The operator can be used to look for multiple items at once:

twig
{% 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.

twig
{% if 2 is even %}
    ...
{% endif %}

The result can be negated using the not operator:

twig
{% if 3 is not even %}
    ...
{% endif %}

logic

The and operator and the or operator are available to join boolean expressions.

twig
{% if 2 is even and 3 is odd %}
    ...
{% endif %}

The not operator is available to negate a boolean expression.

twig
{% if 3 is not even %}
    ...
{% endif %}

Parenthesis can be used to group expressions to ensure a desired precedence.

twig
{% 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.

twig
{{ 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.

twig
{% if 3 is not even %}
    ...
{% endif %}

null-coalescing

Pebble supports the use of the null-coalescing operator that allows testing if variables are defined.

twig
{% set baz = "baz" %}
{{ foo ?? bar ?? baz }}

{# results in: 'baz' #}

{{ foo ?? bar ?? raise }}
{# results: an exception because none of the 3 vars is defined  #}

ternary-operator

Pebble supports the use of the conditional operator (often named the ternary operator).

twig
{{ foo == null ? bar : baz }}

Was this page helpful?