yakub/simple-templating
Composer 安装命令:
composer require yakub/simple-templating
包简介
Simple templating for php
README 文档
README
Simple templating for PHP. You can replace strings and use basic PHP functions.
Build status
Install
The recommended way to install is via Composer:
composer require yakub/simple-templating
Example
Simple usage
<?php $output = \Yakub\SimpleTemplating\Replace::compile( 'Hi {{userName}}, you selected this options {{fn.implode(', ', options)}}', [ 'userName' => 'Jakub' 'options' => [ 'PHP', 'SQL' ] ] ); echo $output; // Output: "Hi Jakub, you selected this options PHP, SQL"
Parameters
\Yakub\SimpleTemplating\Replace::compile($template, $scope, $flags);
- $template - String template using syntax
- $scope - Associative array
- $flags - Additional options for replacing - USE_URLENCODE - Every replaced string will be encoded by
rawurlencode()
Syntax
Available syntax examples
String data
Scope data:
["name" => "Jakub Miškech"]
- '{{name}}' -> 'Jakub Miškech'
- '{{fn.strtoupper(name)}}' - 'JAKUB MIŠKECH'
- '{{name[0]}}' - 'J'
- '{{fn.ucwords('hello world')}}' - 'Hello World'
- '{{fn.urlencode(name)}}' - 'Jakub+Mi%C5%A1kech'
Time data
Scope data:
["strtotimeValue" => "yesterday"]
- '{{fn.date('Y-m-d H:i:s')}}' -> '2020-04-27 17:01:31'
- '{{fn.time()}}' -> '1580140891'
- '{{fn.strtotime(strtotimeValue)}}' -> '1579993200'
- '{{fn.strtodate('yesterday')}}' -> '2020-04-26 00:00:00'
- '{{fn.strtodate('yesterday', 'Y-m-d')}}' -> '2020-01-26'
Number data
Scope data:
["numberA" => "1.4"]
- '{{fn.round(1.5)}}' -> '2'
- '{{fn.round(numberA)}}' -> '1'
- '{{fn.rand(0, 100)}}' -> '41'
Array data
Scope data:
[ "map" => ["typePK" => "name"], "displayName" => "title", "values" => [1, 2, 3], "forExplode" => "hi,hello", "types" => [ ["name" => "php", "title" => "PHP: Hypertext Preprocessor", ["name" => "pg", "title" => "PostgreSQL"] ] ]
- '{{fn.implode(', ', values)}}' -> '1, 2, 3, 4'
- '{{values[0]}}' -> '1'
- '{{fn.implode(', ', fn.array_column(statuses, 'title'))}}' -> 'PHP: Hypertext Preprocessor, PostgreSQL'
- '{{fn.explode(',', forExplode)[1]}}' -> 'hello'
- '{{statuses[0]['title']}}' -> 'PHP: Hypertext Preprocessor'
- '{{statuses[1][displayName]}}' -> 'PostgreSQL'
- '{{statuses[1][map.statusesPK]}}' -> 'pg'
Functions
All available function. More in php.net
-
Number
- round, rand, pow, floor, abs
-
Date time
- time, date, gmdate, strtotime, strtodate*
-
Array
- explode, implode, array_column, array_push, array_merge
-
String
- trim, strlen, substr, strpos, strstr, sprintf, ucfirst, ucwords, strtoupper, strtolower, strip_tags, str_replace, urlencode, rawurlencode
Arithmetic operators
Arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication and division.
Scope data:
["done" => 9, "total" => 100, "float" => "5.4" ]
- '{{1 + 2}}' -> '3'
- '{{1 + 2 * 3}}' -> '7'
- '{{(1 + 2) * 3}}' -> '9'
- '{{(1.2 + 2,4) * float}}' -> '19.44'
- '{{fn.round((done / total)*100, 2)}} %' -> '9 %'
Conditions
Syntax allow use inline condition with using logical and comparison operators.
Scope data:
["success" => true, "ok" => "ok", "notOk" => "not ok" ]
- '{{(success) ? ok : notOk)}}' -> 'ok'
- '{{(! success) ? ok : notOk)}}' -> 'not ok'
- '{{1 && (0 || success) ? ok : notOk)}}' -> 'ok'
- '{{1 > 0 && 1 < 1 ? ok : notOk)}}' -> 'not ok'
Advanced
You can use this syntax to get access to object value. This work only when you use in template one replacing syntax. Value will be stored in onlyOneParamValue attribute.
<?php $output = \Yakub\SimpleTemplating\Replace::compile( '{{types[1]}}', [ "types" => [ ["name" => "php", "title" => "PHP: Hypertext Preprocessor", ["name" => "pg", "title" => "PostgreSQL"] ] ] ); echo $output; // Output: "" array isn't transformed to string echo json_encode($output->onlyOneParamValue); // Output: "{"name":"pg","title":"PostgreSQL"}"
yakub/simple-templating 适用场景与选型建议
yakub/simple-templating 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 04 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 yakub/simple-templating 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yakub/simple-templating 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2020-04-28