format_list
3.29
The format_list filter was added in Twig 3.29.
Note
The format_list filter requires the IntlListFormatter class, which is available since PHP 8.5.
The format_list filter formats a list of strings as a single, locale-aware string:
1 2
{# en: Apple, Banana, and Cherry #}
{{ ['Apple', 'Banana', 'Cherry']|format_list }}
The list of supported types:
and: Formats the list as a conjunction (default):1 2
{# en: Apple, Banana, and Cherry #} {{ ['Apple', 'Banana', 'Cherry']|format_list(type: 'and', locale: 'en') }}or: Formats the list as a disjunction:1 2
{# en: Apple, Banana, or Cherry #} {{ ['Apple', 'Banana', 'Cherry']|format_list(type: 'or', locale: 'en') }}units: Formats the list as a compound unit, without implying a conjunction or disjunction:1 2
{# en: Apple, Banana, Cherry #} {{ ['Apple', 'Banana', 'Cherry']|format_list(type: 'units', locale: 'en') }}
The list of supported widths:
wide: The full-length form (default):1 2
{# en: Apple, Banana, and Cherry #} {{ ['Apple', 'Banana', 'Cherry']|format_list(width: 'wide', locale: 'en') }}short: A shorter form:1 2
{# en: Apple, Banana, & Cherry #} {{ ['Apple', 'Banana', 'Cherry']|format_list(width: 'short', locale: 'en') }}narrow: The most compact form:1 2
{# en: Apple, Banana, Cherry #} {{ ['Apple', 'Banana', 'Cherry']|format_list(width: 'narrow', locale: 'en') }}
By default, the filter uses the current locale. You can pass it explicitly:
1 2
{# fr: Apple, Banana et Cherry #}
{{ ['Apple', 'Banana', 'Cherry']|format_list(locale: 'fr') }}
Note
The format_list filter is part of the IntlExtension which is not installed by default. Install it first:
1
$ composer require twig/intl-extra
Then, on Symfony projects, install the twig/extra-bundle:
1
$ composer require twig/extra-bundle
Otherwise, add the extension explicitly on the Twig environment:
1 2 3 4
use Twig\Extra\Intl\IntlExtension;
$twig = new \Twig\Environment(...);
$twig->addExtension(new IntlExtension());