okneloper/placeholders
Composer 安装命令:
composer require okneloper/placeholders
包简介
Batch replace placeholders in a (HTML) string.
README 文档
README
Batch replace placeholders in a (HTML) string.
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.
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.
#Usage
$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> */
See the full example for details of implementation.
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); }
统计信息
- 总下载量: 17
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-11-06