dhii/delimited-token-template
Composer 安装命令:
composer require dhii/delimited-token-template
包简介
A flexible implementation for handlebars-style templates
README 文档
README
A flexible implementation for handlebars-style templates.
Details
This template implementation is useful if you need to replace simple tokens with values. Good examples are emails, which contain text like "Hello, %username%", or paths with variable segments, such as "/users/:username/profile".
The template supports any string delimiters: '[*placeholder*]', '~placeholder', and "{{placeholder}}" will all work. It's possible to have the left delimiter different from the right one, have delimiters which contain more than one character, or to even omit one of the delimiters.
When two delimiters are used, they can be escaped by a configurable escape character
to make them be used literally in the rendered result. With the left and right
delimiters being "{{" and "}}" respectively, and the escape char being "",
the string "{{ {{username}}" could produce "{{ johnny" if rendered with context
['username' => 'johnny']. If the second left delimiter was escaped instead,
i.e. "Hello, {{ {{username}}", rendering with [' {{username' => 'johnny'] would
produce "Hello, johnny". This demonstrates that escaped delimiters will be replaced
with their literal value in both the end result, and in token names.
Using one delimiter is also possible, e.g. "Hello, :username!" would produce
"Hello, johnny!" when rendered with context ['username' => 'johnny']. In this
case, however, contrary to using two delimiters, token names are limited to
alphanumeric chars, as well as '_', '-', and '.' (underscore, dash, and period).
In addition, it is not possible to escape delimiters. Consequently, token names
cannot contain delimiters.
Usage
Two delimiters
This example uses two identical delimiters, and an escaped delimiter in the middle of the token name.
use Dhii\Output\DelimitedTokenTemplate\Template; $template = new Template('Hello, %user\%name%!', '%', '%', '\\'); // Note escaped delimiter $template->render(['user%name' => 'johnny']); // Hello, johnny!
Different long delimiters
This example uses two different delimiters that are longer than 1 character, and of different lengths.
use Dhii\Output\DelimitedTokenTemplate\Template; $template = new Template('Hello, -user\-name__!', '-', '__', '\\'); // Note completely different delimiters $template->render(['user-name' => 'johnny']); // Hello, johnny!
One delimiter with factory
Often times there will be some kind of convention within your application with regard to delimiters and the escape character. This these cases, it often useful to be able to instantiate several templates based on that standard, without having to specify it every time. The factory can represent that convention. This example uses tokens with only the left delimiter.
use Dhii\Output\DelimitedTokenTemplate\TemplateFactory; $factory= new TemplateFactory(':', '', ''); // Note absence of right delimiter and escape character $profilePath = $factory->fromString('/users/:username/profile') ->render(['username' => 'johnny']); // '/users/johnny/profile' $settingsPath = $factory->fromString('/users/:userId/settings') ->render(['userId' => '1234']); // '/users/1234/settings'
dhii/delimited-token-template 适用场景与选型建议
dhii/delimited-token-template 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35.96k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 03 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 dhii/delimited-token-template 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dhii/delimited-token-template 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 35.96k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-11