Twig

The flexible, fast, and secure
template engine for PHP

a Symfony Product
Docs Filters format_list
Docs for Twig version 3.x
Switch to another version: 1.x, 2.x

Questions & Feedback

License

Twig documentation is licensed under the new BSD license.

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());

Arguments

  • type: The type of the list output
  • width: The width of the list output
  • locale: The locale code as defined in RFC 5646