PHP code example of okneloper / placeholders
1. Go to this page and download the library: Download okneloper/placeholders library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
okneloper / placeholders example snippets
$content = '
<div class="intro-message">
<h1>:#:home.h1:#:</h1>
<h3>:#:home.h2:#:</h3>
</div>
';
$content = $processor->process($content);
echo $content;
/*
<div class="intro-message">
<h1>Welcome to the site!</h1>
<h3>Slogan goes here</h3>
</div>
*/
/**
* 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);
}