mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 12:26:30 +00:00
871747851f
* 2.x: Fix map example output
35 lines
809 B
ReStructuredText
35 lines
809 B
ReStructuredText
``map``
|
|
=======
|
|
|
|
The ``map`` filter applies an arrow function to the elements of a sequence or a
|
|
mapping. The arrow function receives the value of the sequence or mapping:
|
|
|
|
.. code-block:: twig
|
|
|
|
{% set people = [
|
|
{first: "Bob", last: "Smith"},
|
|
{first: "Alice", last: "Dupond"},
|
|
] %}
|
|
|
|
{{ people|map(p => "#{p.first} #{p.last}")|join(', ') }}
|
|
{# outputs Bob Smith, Alice Dupond #}
|
|
|
|
The arrow function also receives the key as a second argument:
|
|
|
|
.. code-block:: twig
|
|
|
|
{% set people = {
|
|
"Bob": "Smith",
|
|
"Alice": "Dupond",
|
|
} %}
|
|
|
|
{{ people|map((value, key) => "#{key} #{value}")|join(', ') }}
|
|
{# outputs Bob Smith, Alice Dupond #}
|
|
|
|
Note that the arrow function has access to the current context.
|
|
|
|
Arguments
|
|
---------
|
|
|
|
* ``arrow``: The arrow function
|