chrissileinus/template-php
Composer 安装命令:
composer require chrissileinus/template-php
包简介
A template engine to generate strings from a template. Also it is possible to colorize parts with ANSI codes with the help of the ansiPHP class.
README 文档
README
A template engine to generate strings from a template. It is also prossible to colorize parts with ANSI codes with the help of the ansiPHP class.
Why
On the search of a simple template engine to generate some output on the bash i found nicmart/StringTemplate. But in the need for static callable methods with colorizing features, I decided to write something similar.
Usage
placeholders
Placeholders are delimited by default by { and }, but you can specify others by Str::placeholders().
Chrissileinus\Template\Str::placeholders('a', 'b');
replace
If array key is not specified then write the index into the template.
// @returns string: "This is my value: nic" Chrissileinus\Template\Str::replace("This is my value: {0}", ['nic']);
With specified keys are practical on longer templates.
// @returns string: "My name is Christian Backus" Chrissileinus\Template\Str::replace( "My name is {name} {surname}", [ 'name' => 'Christian', 'surname' => 'Backus' ] );
You are also able to access nested arrays.
// @returns string: "My name is Christian and her name is Aline" Chrissileinus\Template\Str::replace( "My name is {my.name} and her name is {her.name}", [ 'my' => ['name' => 'Christian'], 'her' => ['name' => 'Aline'] ] );
You are also able to submit several arrays. All arrays get merged by array_replace_recursive with the given order.
// @returns string: "My name is Christian and her name is Aline" Chrissileinus\Template\Str::replace( "My name is {my.name} and her name is {her.name}", [ 'my' => ['name' => 'Christian'] ], [ 'her' => ['name' => 'Aline'] ] );
replaceFormat | replaceF
With the usage of sprintf we are able to use the convertion specifications for the placeholders.
// @returns string: "My name is chris and my age is 041 [ 83.40]" Chrissileinus\Template\Str::replaceFormat( "My name is {user.name} and my age is {age%03d} [{weight%6.2f}]", [ 'user' => [ 'name' => "chris" ], 'age' => 41, 'weight' => 83.4, ] );
Additionally we also able to colorize the result with the ANSI Commands. Helpful for output in terminal applications.
Just a additional & and a list of color and styling commands. They can also be spezified in the input array.
// @returns string: "My name is chris and my age is 041 [ 83.40]" Chrissileinus\Template\Str::replaceFormat( "My name is {user.name&user.color} and {my&user.color} age is {age&f_blue} {[{weight%6.2f}]&test.style}", [ 'user' => [ 'name' => "chris" ], 'age' => 41, 'weight' => 83.4, ], [ 'user' => [ 'color' => "f_yellow" ], 'test' => [ 'style' => "bold,f_magenta" ] ] );
chrissileinus/template-php 适用场景与选型建议
chrissileinus/template-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 11 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 chrissileinus/template-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chrissileinus/template-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 22
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-05