Skip to content

Commit

Permalink
Tweak docs about operators
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Apr 28, 2024
1 parent 8766c2c commit e8df2b7
Showing 1 changed file with 67 additions and 55 deletions.
122 changes: 67 additions & 55 deletions doc/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -512,53 +512,6 @@ Expressions

Twig allows expressions everywhere.

.. note::

Twig uses operators to perform various operations within templates. Understanding
the precedence of these operators is crucial for writing correct and efficient Twig templates.

The operator precedence is as follows, with the lowest-precedence operators
listed first:

============================= =================================== ===================================================
Operator Score of precedence Description
============================= =================================== ===================================================
``?:`` Perfoms a ternary, conditional statement.
``or`` 10 Performs a logical OR operation between two
boolean expressions.
``and`` 15 Performs a logical AND operation between two
boolean expressions.
``b-or`` 16 Performs a bitwise OR operation on integers.
``b-xor`` 17 Performs a bitwise XOR operation on integers.
``b-and`` 18 Performs a bitwise AND operation on integers
``==``, ``!=``, ``<=>``, 20 Comparison Operators: Compare values and check
``<``, ``>``, ``>=``, for containment, pattern matching, etc.
``<=``, ``not in``, ``in``,
``matches``, ``starts with``,
``ends with``, ``has some``,
``has every``
``..`` 25 Creates a range of values, commonly used in loops.
``+``, ``-`` 30 Performs operations on numbers.
``~`` 40 Concatenates strings together.
``*``, ``/``, ``//``, ``%`` 60 Handles arithmetic operations on numbers.
``is``, ``is not`` 100 Tests
``**`` 200 Raises a number to the power of another.
``??`` 300 Handles cases where a variable might be null.
``|``,``[]``,``.`` Filters are evaluated first
============================= =================================== ===================================================

This means that ``{{ 6 b-and 2 or 6 b-and 16 }}`` results in ``(6 & 2) || (6 & 16)``.

.. code-block:: twig
{% set greeting = 'Hello ' %}
{% set name = 'Fabien' %}
{{ greeting ~ name|lower }} {# Hello fabien #}
{# use parenthesis to change precedence #}
{{ (greeting ~ name)|lower }} {# hello fabien #}
Literals
~~~~~~~~

Expand Down Expand Up @@ -622,6 +575,20 @@ Arrays and hashes can be nested:
but :ref:`string interpolation <templates-string-interpolation>` is only
supported in double-quoted strings.

.. _templates-string-interpolation:

String Interpolation
~~~~~~~~~~~~~~~~~~~~

String interpolation (``#{expression}``) allows any valid expression to appear
within a *double-quoted string*. The result of evaluating that expression is
inserted into the string:

.. code-block:: twig
{{ "foo #{bar} baz" }}
{{ "foo #{1 + 2} baz" }}
Math
~~~~

Expand Down Expand Up @@ -826,19 +793,64 @@ The following operators don't fit into any of the other categories:
{% set numbers = [1, 2, ...moreNumbers] %}
{% set ratings = {'foo': 10, 'bar': 5, ...moreRatings} %}
.. _templates-string-interpolation:
String Interpolation
~~~~~~~~~~~~~~~~~~~~
Operators
~~~~~~~~~

Twig uses operators to perform various operations within templates.
Understanding the precedence of these operators is crucial for writing correct
and efficient Twig templates.

The operator precedence rules are as follows, with the lowest-precedence
operators listed first:

============================= =================================== =====================================================
Operator Score of precedence Description
============================= =================================== =====================================================
``?:`` 0 Ternary operator, conditional statement
``or`` 10 Logical OR operation between two boolean expressions
``and`` 15 Logical AND operation between two boolean expressions
``b-or`` 16 Bitwise OR operation on integers
``b-xor`` 17 Bitwise XOR operation on integers
``b-and`` 18 Bitwise AND operation on integers
``==``, ``!=``, ``<=>``, 20 Comparison operators
``<``, ``>``, ``>=``,
``<=``, ``not in``, ``in``,
``matches``, ``starts with``,
``ends with``, ``has some``,
``has every``
``..`` 25 Range of values
``+``, ``-`` 30 Addition and substraction on numbers
``~`` 40 String concatenation
``not`` 50 Negates a statement
``*``, ``/``, ``//``, ``%`` 60 Arithmetic operations on numbers
``is``, ``is not`` 100 Tests
``**`` 200 Raises a number to the power of another
``??`` 300 Default value when a variable is null
``+``, ``-`` 500 Unary operations on numbers
``|``,``[]``,``.`` - Filters, array, hash, and attribute access
============================= =================================== =====================================================

Without using any parentheses, the operator precedence rules are used to
determine how to convert the code to PHP:

String interpolation (``#{expression}``) allows any valid expression to appear
within a *double-quoted string*. The result of evaluating that expression is
inserted into the string:
.. code-block:: twig
{{ 6 b-and 2 or 6 b-and 16 }}
{# it is converted to the following PHP code: (6 & 2) || (6 & 16) #}
Change the default precedence by explicitely grouping expressions with parentheses:

.. code-block:: twig
{{ "foo #{bar} baz" }}
{{ "foo #{1 + 2} baz" }}
{% set greeting = 'Hello ' %}
{% set name = 'Fabien' %}
{{ greeting ~ name|lower }} {# Hello fabien #}
{# use parenthesis to change precedence #}
{{ (greeting ~ name)|lower }} {# hello fabien #}
.. _templates-whitespace-control:

Expand Down

0 comments on commit e8df2b7

Please sign in to comment.