Twig

The flexible, fast, and secure
template engine for PHP

a Symfony Product
You are reading the documentation for Twig 2.x. Switch to the documentation for Twig 1.x, 3.x.
Warning Twig version 2.x is no longer maintained.

Questions & Feedback

License

Twig documentation is licensed under the new BSD license.

map

2.10

The map filter was added in Twig 2.10.

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:

1
2
3
4
5
6
7
{% 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:

1
2
3
4
5
6
7
{% 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