From 459ed67e4889f2e8ec4f7073757bda378e63da31 Mon Sep 17 00:00:00 2001 From: "hubert.lenoir" Date: Wed, 28 Dec 2022 14:34:31 +0100 Subject: [PATCH] Add "has some" and "has every" expressions --- doc/templates.rst | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/doc/templates.rst b/doc/templates.rst index 380d42b4f..580458cf7 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -505,7 +505,7 @@ Twig allows expressions everywhere. The operator precedence is as follows, with the lowest-precedence operators listed first: ``?:`` (ternary operator), ``b-and``, ``b-xor``, ``b-or``, ``or``, ``and``, ``==``, ``!=``, ``<=>``, ``<``, ``>``, ``>=``, ``<=``, - ``in``, ``matches``, ``starts with``, ``ends with``, ``..``, ``+``, ``-``, + ``in``, ``matches``, ``starts with``, ``ends with``, ``has every``, ``has some``, ``..``, ``+``, ``-``, ``~``, ``*``, ``/``, ``//``, ``%``, ``is`` (tests), ``**``, ``??``, ``|`` (filters), ``[]``, and ``.``: @@ -661,6 +661,20 @@ next section). {% if phone matches '/^[\\d\\.]+$/' %} {% endif %} +Check that a sequence or a mapping ``has every`` or ``has some`` of its elements +``true`` using an arrow function. The arrow function receives the value of the +sequence or mapping. + +.. code-block:: twig + + {% set sizes = [34, 36, 38, 40, 42] %} + + {% set hasOnlyOver38 = sizes has every v => v > 38 %} + {# hasOnlyOver38 is false #} + + {% set hasOver38 = sizes has some v => v > 38 %} + {# hasOver38 is true #} + Containment Operator ~~~~~~~~~~~~~~~~~~~~