okneloper/placeholders 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-11-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固