Skip to content

Commit

Permalink
chore: add zh-CN readme, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 11, 2021
1 parent 1df2b2a commit 95420b7
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 6 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -169,14 +171,19 @@ $tpl->addDirective(
);
```

In template can use:
Use in template:

```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)
188 changes: 188 additions & 0 deletions README.zh-CN.md
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)

0 comments on commit 95420b7

Please sign in to comment.