From 95420b75848484de2ab55b84abd994a93acb2739 Mon Sep 17 00:00:00 2001 From: Inhere Date: Thu, 11 Nov 2021 13:36:31 +0800 Subject: [PATCH] chore: add zh-CN readme, update readme --- README.md | 19 +++-- README.zh-CN.md | 188 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 201 insertions(+), 6 deletions(-) create mode 100644 README.zh-CN.md diff --git a/README.md b/README.md index cd9e14a..b05e785 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,14 @@ ⚡️ Simple and fastly template engine for PHP +> **[中文说明](README.zh-CN.md)** + ## Features -- It's simple and fastly. +- It's simple, lightweight and fastly. - It is simply processed and converted into native PHP syntax -- simple echo syntax. eg: `{{= $var }}` `{{ $var }}` `{{ echo $var }}` -- chained access array value syntax. eg: `{{ $arr.0 }}` `{{ $map.name }}` `{{ $map.user.name }}` +- support simple echo print syntax. eg: `{{= $var }}` `{{ $var }}` `{{ echo $var }}` +- support chained access array value. eg: `{{ $arr.0 }}` `{{ $map.name }}` `{{ $map.user.name }}` - support php builtin function as filters. eg: `{{ $var | ucfirst }}` - support all control syntax. such as `if,elseif,else;foreach;for;switch` - support add custom filters. @@ -83,11 +85,11 @@ First value is: {{ $arr.0 }} ## Use Filters -Default builtin filters: +Default built-in filters: - `upper` - equals `strtoupper` - `lower` - equals `strtolower` -- `nl` append newline `\n` +- `nl` - append newline `\n` ### Using the filters @@ -169,7 +171,7 @@ $tpl->addDirective( ); ``` -In template can use: +Use in template: ```php @@ -177,6 +179,11 @@ In template can use: ``` +## Dep packages + +- [toolkit/fsutil](https://github.com/php-toolkit/fsutil) +- [toolkit/stdlib](https://github.com/php-toolkit/stdlib) + ## License [MIT](LICENSE) diff --git a/README.zh-CN.md b/README.zh-CN.md new file mode 100644 index 0000000..6222809 --- /dev/null +++ b/README.zh-CN.md @@ -0,0 +1,188 @@ +# EasyTpl + +[![License](https://img.shields.io/packagist/l/phppkg/easytpl.svg?style=flat-square)](LICENSE) +[![Php Version](https://img.shields.io/badge/php-%3E=8.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/phppkg/easytpl) +[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/phppkg/easytpl)](https://github.com/phppkg/easytpl) +[![Actions Status](https://github.com/phppkg/easytpl/workflows/Unit-Tests/badge.svg)](https://github.com/phppkg/easytpl/actions) + +⚡️ 简单快速的 PHP 模板引擎 + +> **[EN-README](README.md)** + +## 功能特性 +- 简单、轻量且快速。 + - 仅仅简单处理并转换为原生PHP语法 +- 支持简单的输出语法。 例如:`{{= $var }}` `{{ $var }}` `{{ echo $var }}` +- 支持所有控制语法。 如 `if,elseif,else;foreach;for;switch` +- 支持链式访问数组值。 例如:`{{ $arr.0 }}` `{{ $map.name }}` `{{ $map.user.name }}` +- 支持使用 PHP 内置函数作为过滤器。 例如:`{{ $var | ucfirst }}` +- 支持添加自定义过滤器 + - 默认内置过滤器:`upper` `lower` `nl` +- 支持添加自定义指令 + +## 安装 + +**composer** + +```bash +composer require phppkg/easytpl +``` + +## 快速开始 + +```php +use PhpPkg\EasyTpl\EasyTemplate; + +$tplCode = <<<'CODE' +My name is {{ $name | strtoupper }}, +My develop tags: +{{ foreach($tags => $tag) }} +- {{ $tag }} + +{{ endforeach }} +CODE; + +$t = new EasyTemplate(); + +$str = $t->renderString($tplCode, [ + 'name' => 'inhere', + 'tags' => ['php', 'go', 'java'], +]); + +echo $str; +``` + +**Output**: + +```text +My name is INHERE, +My develop tags: +- php +- go +- java +``` + +### More usage + +**chained access array** + +Use `.` access array value. + +```php +$arr = [ + 'val0', + 'subKey' => 'val1', +]; +``` + +在模板中使用: + +```php +First value is: {{ $arr.0 }} +'subKey' value is: {{ $arr.subKey }} +``` + +## 使用过滤器 + +默认内置过滤器: + +- `upper` - 等同于 `strtoupper` +- `lower` - 等同于 `strtolower` +- `nl` - 追加换行符 `\n` + +### 过滤器使用示例 + +您可以在任何模板中使用过滤器。 + +**基本使用**: + +```php +{{ 'john' | ucfirst }} // John +``` + +**链式使用**: + +```php +{{ 'john' | ucfirst | substr:0,1 }} // J +{{ '1999-12-31' | date:'Y/m/d' }} // 1999/12/31 +``` + +**传递非静态值**: + +```php +{{ $name | ucfirst | substr:0,1 }} +{{ $user['name'] | ucfirst | substr:0,1 }} +{{ $userObj->name | ucfirst | substr:0,1 }} +{{ getName() | ucfirst | substr:0,1 }} +``` + +**将变量作为过滤器参数传递**: + +```php +{{ + $suffix = '¥'; +}} + +{{ '12.75' | add_suffix:$suffix }} // 12.75¥ +``` + +### 自定义过滤器 + +```php +use PhpPkg\EasyTpl\EasyTemplate; + +$tpl = EasyTemplate::new(); +// use php built function +$tpl->addFilter('upper', 'strtoupper'); + +// add more +$tpl->addFilters([ + 'last3chars' => function (string $str): string { + return substr($str, -3); + }, +]); +``` + +在模板中使用: + +```php +{{ + $name = 'inhere'; +}} + +{{ $name | upper }} // Output: INHERE +{{ $name | last3chars }} // Output: ere +{{ $name | last3chars | upper }} // Output: ERE +``` + +## 自定义指令 + +您可以使用指令实现一些特殊的逻辑。 + +```php +$tpl = EasyTemplate::new(); +$tpl->addDirective( + 'include', + function (string $body, string $name) { + /** will call {@see EasyTemplate::include()} */ + return '$this->' . $name . $body; + } +); +``` + +在模板中使用: + +```php + +{{ include('part/header.tpl', ['title' => 'My world']) }} + +``` + +## Dep packages + +- [toolkit/fsutil](https://github.com/php-toolkit/fsutil) +- [toolkit/stdlib](https://github.com/php-toolkit/stdlib) + +## License + +[MIT](LICENSE)