Skip to content

Commit

Permalink
chore: update some comments, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 11, 2021
1 parent 95420b7 commit 7719966
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# EasyTpl

[![License](https://img.shields.io/packagist/l/phppkg/easytpl.svg?style=flat-square)](LICENSE)
[![License](https://img.shields.io/github/license/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)
Expand All @@ -13,6 +13,7 @@

- It's simple, lightweight and fastly.
- It is simply processed and converted into native PHP syntax
- Compatible with PHP native syntax
- 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 }}`
Expand Down Expand Up @@ -65,9 +66,22 @@ My develop tags:

### More usage

**config template**

```php
use PhpPkg\EasyTpl\EasyTemplate;

$t = EasyTemplate::new([
'tplDir' => 'path/to/templates',
'allowExt' => ['.php', '.tpl'],
]);

// do something ...
```

**chained access array**

Use `.` access array value.
Can use `.` to quick access array value.

```php
$arr = [
Expand Down
44 changes: 30 additions & 14 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# EasyTpl

[![License](https://img.shields.io/packagist/l/phppkg/easytpl.svg?style=flat-square)](LICENSE)
[![License](https://img.shields.io/github/license/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)
Expand All @@ -9,9 +9,11 @@

> **[EN-README](README.md)**
## 功能特性
## 功能特性

- 简单、轻量且快速。
- 仅仅简单处理并转换为原生PHP语法
- 兼容PHP原生语法使用
- 支持简单的输出语法。 例如:`{{= $var }}` `{{ $var }}` `{{ echo $var }}`
- 支持所有控制语法。 如 `if,elseif,else;foreach;for;switch`
- 支持链式访问数组值。 例如:`{{ $arr.0 }}` `{{ $map.name }}` `{{ $map.user.name }}`
Expand Down Expand Up @@ -52,7 +54,7 @@ $str = $t->renderString($tplCode, [
echo $str;
```

**Output**:
**渲染输出**:

```text
My name is INHERE,
Expand All @@ -62,11 +64,24 @@ My develop tags:
- java
```

### More usage
### 更多使用

**配置设置**

```php
use PhpPkg\EasyTpl\EasyTemplate;

$t = EasyTemplate::new([
'tplDir' => 'path/to/templates',
'allowExt' => ['.php', '.tpl'],
]);

// do something ...
```

**chained access array**
**快速访问数组值**

Use `.` access array value.
可以使用`.` 来快速访问数组值。

```php
$arr = [
Expand All @@ -78,8 +93,8 @@ $arr = [
在模板中使用:

```php
First value is: {{ $arr.0 }}
'subKey' value is: {{ $arr.subKey }}
first value is: {{ $arr.0 }} // val0
'subKey' value is: {{ $arr.subKey }} // val1
```

## 使用过滤器
Expand All @@ -97,13 +112,14 @@ First value is: {{ $arr.0 }}
**基本使用**:

```php
{{ 'john' | ucfirst }} // John
{{ 'inhere' | ucfirst }} // Inhere
{{ 'inhere' | upper }} // INHERE
```

**链式使用**:

```php
{{ 'john' | ucfirst | substr:0,1 }} // J
{{ 'inhere' | ucfirst | substr:0,2 }} // In
{{ '1999-12-31' | date:'Y/m/d' }} // 1999/12/31
```

Expand All @@ -113,7 +129,7 @@ First value is: {{ $arr.0 }}
{{ $name | ucfirst | substr:0,1 }}
{{ $user['name'] | ucfirst | substr:0,1 }}
{{ $userObj->name | ucfirst | substr:0,1 }}
{{ getName() | ucfirst | substr:0,1 }}
{{ $userObj->getName() | ucfirst | substr:0,1 }}
```

**将变量作为过滤器参数传递**:
Expand Down Expand Up @@ -150,9 +166,9 @@ $tpl->addFilters([
$name = 'inhere';
}}

{{ $name | upper }} // Output: INHERE
{{ $name | last3chars }} // Output: ere
{{ $name | last3chars | upper }} // Output: ERE
{{ $name | upper }} // INHERE
{{ $name | last3chars }} // ere
{{ $name | last3chars | upper }} // ERE
```

## 自定义指令
Expand Down
4 changes: 2 additions & 2 deletions src/AbstractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
abstract class AbstractTemplate implements TemplateInterface
{
/**
* @var array
* @var array<string, mixed>
*/
protected array $globalVars = [];

Expand Down Expand Up @@ -49,7 +49,7 @@ abstract class AbstractTemplate implements TemplateInterface
public $pathResolver;

/**
* @param array $config
* @param array{tplDir: string, allowExt: array, globalVars: array} $config
*
* @return static
*/
Expand Down
3 changes: 2 additions & 1 deletion src/EasyTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ class EasyTemplate extends PhpTemplate implements EasyTemplateInterface
/**
* Class constructor.
*
* @param array{compiler: class-string|CompilerInterface} $config
* @param array{tmpDir: string, compiler: class-string|CompilerInterface} $config
*/
public function __construct(array $config = [])
{
// custom compiler
if (isset($config['compiler'])) {
$customCompiler = $config['compiler'];
// class-string
Expand Down
2 changes: 1 addition & 1 deletion src/PhpTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class PhpTemplate extends AbstractTemplate
{
/**
* The tmp dir for auto generated temp php file
* The cache dir for generated temp php file
*
* @var string
*/
Expand Down

0 comments on commit 7719966

Please sign in to comment.