This commit is contained in:
Fabien Potencier
2024-06-18 07:24:08 +02:00
parent c44be99a84
commit bc19f2f48e
4 changed files with 23 additions and 11 deletions
+5 -9
View File
@@ -1,9 +1,9 @@
``merge`` ``merge``
========= =========
The ``merge`` filter merges sequences and mappings. The ``merge`` filter merges sequences and mappings:
The ``merge`` filter also works on sequences: For sequences, new values are added at the end of the existing ones:
.. code-block:: twig .. code-block:: twig
@@ -13,9 +13,9 @@ The ``merge`` filter also works on sequences:
{# values now contains [1, 2, 'apple', 'orange'] #} {# values now contains [1, 2, 'apple', 'orange'] #}
New values are added at the end of the existing ones. For mappings, the merging process occurs on the keys; if the key does not
already exist, it is added but if the key already exists, its value is
The ``merge`` filter also works on mappings: overridden:
.. code-block:: twig .. code-block:: twig
@@ -25,10 +25,6 @@ The ``merge`` filter also works on mappings:
{# items now contains {'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car', 'renault': 'car'} #} {# items now contains {'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car', 'renault': 'car'} #}
For mappings, the merging process occurs on the keys: if the key does not
already exist, it is added but if the key already exists, its value is
overridden.
.. tip:: .. tip::
If you want to ensure that some values are defined in a mapping (by given If you want to ensure that some values are defined in a mapping (by given
@@ -0,0 +1,8 @@
--TEST--
"cycle" function returns an error on empty mappings
--TEMPLATE--
{{ cycle({}, 0) }}
--DATA--
return []
--EXCEPTION--
Twig\Error\RuntimeError: The "cycle" function does not work on empty sequences/mappings in "index.twig" at line 2.
@@ -0,0 +1,8 @@
--TEST--
"cycle" function returns an error on empty sequences
--TEMPLATE--
{{ cycle([], 0) }}
--DATA--
return []
--EXCEPTION--
Twig\Error\RuntimeError: The "cycle" function does not work on empty sequences/mappings in "index.twig" at line 2.
@@ -1,8 +1,8 @@
--TEST-- --TEST--
Twig does not confuse strings with integers in getAttribute() Twig does not confuse strings with integers in getAttribute()
--TEMPLATE-- --TEMPLATE--
{{ hash['2e2'] }} {{ mapping['2e2'] }}
--DATA-- --DATA--
return ['hash' => ['2e2' => 'works']] return ['mapping' => ['2e2' => 'works']]
--EXPECT-- --EXPECT--
works works