Skip to content

Latest commit

 

History

History
53 lines (38 loc) · 1.7 KB

File metadata and controls

53 lines (38 loc) · 1.7 KB

ListModifier

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

Behavior

Array Size Output Format
Empty Delegates to next modifier
1 item apple
2 items apple and banana
3+ items apple, 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

Parameters Template Output
['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.