ListModifier

January 21, 2026 ยท View on GitHub

The |list modifier formats arrays into human-readable lists with conjunctions.

Behavior

Array SizeOutput Format
EmptyDelegates to next modifier
1 itemapple
2 itemsapple and banana
3+ itemsapple, banana, and cherry

Pipes

  • |list or |list:and - Uses :and as conjunction
  • |list:or - Uses :or as conjunction

Usage

use Respect\StringFormatter\PlaceholderFormatter;

$formatter = new PlaceholderFormatter([
    'fruits' => ['apple', 'banana', 'cherry'],
]);

echo $formatter->format('I like {{fruits|list}}');
// Output: I like apple, banana, and cherry

echo $formatter->format('Choose {{fruits|list:or}}');
// Output: Choose apple, banana, or cherry

Examples

ParametersTemplateOutput
['items' => ['a']]{{items|list}}a
['items' => ['a', 'b']]{{items|list}}a and b
['items' => ['a', 'b']]{{items|list:or}}a or b
['items' => ['a', 'b', 'c']]{{items|list:and}}a, b, and c
['items' => ['a', 'b', 'c']]{{items|list:or}}a, b, or c

Known Limitations

This modifier uses the Oxford comma (a comma before the conjunction) for lists with 3+ items. This grammar rule is specific to English and may not work well with other languages.