Batch replace placeholders in a (HTML) string., (*1)
It gives you the full list of placeholders to resolve before replacing. This is handy when you have a lot of placeholders and you want to only fetch those that you need., (*2)
For instance, if your data is stored in a database or returned by a remote API, generally you want to fetch all data in one query/request., (*3)
Usage
$content = '
:#:home.h1:#:
:#:home.h2:#:
';
$content = $processor->process($content);
echo $content;
/*
Welcome to the site!
Slogan goes here
*/
See the full example for details of implementation., (*4)
Example
/**
* Define a placeholder translator by implementing Okneloper\Placeholders\Translator interface
*/
class ExamplePlaceholderTranslater implements Translator
{
/**
* @param PlaceholderCollection $placeholders
* @return array
*/
public function translate($placeholders)
{
$keys = $placeholders->keys();
// fetch placeholders using keys from database
// ...
$data = [
'home.h1' => 'Welcome to the site!',
'home.h2' => 'Slogan goes here',
];
$replacements = [];
foreach ($placeholders as $placeholder) {
$replacements[$placeholder->placeholder] = $data[$placeholder->key];
// optionally apply filters found in $placeholder->filters
}
return $replacements;
}
}
// Batch replace placeholders in a response
/**
* Handle the event.
*
* @param Request $request
* @param Response $response
*/
public function handle(Request $request, Response $response)
{
$content = $response->getContent();
$processor = new Processor(new ExamplePlaceholderTranslater());
$content = $processor->process($content);
$response->setContent($content);
}