generated from inherelab/php-pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add zh-CN readme, update readme
- Loading branch information
Showing
2 changed files
with
201 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |