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``
=========
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
@@ -13,9 +13,9 @@ The ``merge`` filter also works on sequences:
{# values now contains [1, 2, 'apple', 'orange'] #}
New values are added at the end of the existing ones.
The ``merge`` filter also works on mappings:
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:
.. 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'} #}
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::
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--
Twig does not confuse strings with integers in getAttribute()
--TEMPLATE--
{{ hash['2e2'] }}
{{ mapping['2e2'] }}
--DATA--
return ['hash' => ['2e2' => 'works']]
return ['mapping' => ['2e2' => 'works']]
--EXPECT--
works