Modifiers transform placeholder values before they're inserted into the final string. They're applied using the | syntax: {{placeholder|modifier}}.
Modifiers form a chain where each modifier can:
- Handle the value and return a transformed string
- Pass the value to the next modifier in the chain
use Respect\StringFormatter\PlaceholderFormatter;
$formatter = new PlaceholderFormatter([
'name' => 'John',
'items' => ['apple', 'banana'],
]);
echo $formatter->format('Hello {{name}}');
// Output: Hello John
echo $formatter->format('Items: {{items}}');
// Output: Items: ["apple","banana"]You can specify a custom modifier chain when creating a PlaceholderFormatter:
use Respect\StringFormatter\PlaceholderFormatter;
use Respect\StringFormatter\Modifiers\RawModifier;
use Respect\StringFormatter\Modifiers\StringifyModifier;
$formatter = new PlaceholderFormatter(
['name' => 'John', 'active' => true],
new RawModifier(new StringifyModifier()),
);
echo $formatter->format('Hello {{name}}');
// Output: Hello "John"
echo $formatter->format('Hello {{name|raw}}');
// Output: Hello JohnIf no modifier is provided, the formatter uses StringifyModifier by default.
- ListModifier - Formats arrays as human-readable lists with conjunctions
- QuoteModifier - Quotes string values using a stringifier quoter
- RawModifier - Returns scalar values as raw strings with
|rawpipe - StringifyModifier - Always converts values to strings (default)
- StringPassthroughModifier - Returns strings unchanged, delegates non-strings to next modifier
- TransModifier - Translates string values using a Symfony translator
See Creating Custom Modifiers for implementing your own modifiers.