Skip to content

Commit

Permalink
test: update some tests and readme docs
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jan 8, 2023
1 parent 385d887 commit f4415b3
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 13 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
- support add custom filters.
- default builtin filters: `upper` `lower` `nl`
- support add custom directive.
- `EasyTemplate` built in support `include`: `{{ include('parts/header.tpl') }}`
- `EasyTemplate` built in support `layout` `include` `contents`
- `ExtendTemplate` built in support `extends` `block` `endblock`
- support comments in templates. eg: `{{# comments ... #}}`

Expand Down Expand Up @@ -302,6 +302,8 @@ Use in template:

You can use the directives implement some special logic.

>`EasyTemplate` built in support: `layout` `include` `contents`
```php
$tpl = EasyTemplate::new();
$tpl->addDirective(
Expand All @@ -313,6 +315,18 @@ $tpl->addDirective(
);
```

### Use layout

- page template `home01.tpl`

```php
{{ layout('layouts/layout01.tpl') }}

on home: block body;
```

### Use include

**Use in template**

```php
Expand All @@ -334,7 +348,7 @@ New directives:
use PhpPkg\EasyTpl\ExtendTemplate;

$et = new ExtendTemplate();
$et->render('home/index.tpl');
$et->display('home/index.tpl');
```

### Examples for extend
Expand Down Expand Up @@ -378,6 +392,14 @@ footer contents in layout main.
- [toolkit/fsutil](https://github.com/php-toolkit/fsutil)
- [toolkit/stdlib](https://github.com/php-toolkit/stdlib)

## Related

- https://github.com/twigphp/Twig
- https://github.com/nette/latte
- https://github.com/jenssegers/blade
- https://github.com/fenom-template/fenom
- https://github.com/pug-php/pug

## License

[MIT](LICENSE)
6 changes: 3 additions & 3 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
- 支持添加自定义过滤器
- 默认内置过滤器:`upper` `lower` `nl`
- 支持添加自定义指令,提供自定义功能
- `EasyTemplate` 内置支持 `include` `contents`
- `ExtendTemplate` 提供模板继承功能. 内置支持 `extends` `block` `endblock`
- `EasyTemplate` 支持使用布局文件. 支持指令: `layout` `include` `contents`
- `ExtendTemplate` 提供模板继承功能. 支持指令: `extends` `block` `endblock`
- 支持模板中添加注释。 例如: `{{# comments ... #}}`

## 安装
Expand Down Expand Up @@ -330,7 +330,7 @@ $tpl->addDirective('include',function (string $body, string $name) {
use PhpPkg\EasyTpl\ExtendTemplate;

$et = new ExtendTemplate();
$et->render('home/index.tpl');
$et->display('home/index.tpl');
```

### `extend` 模板使用示例
Expand Down
6 changes: 5 additions & 1 deletion test/EasyTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,10 @@ public function testEasy_useLayout01(): void

$t = $this->newTemplate();
$s = $t->render($tplFile);
vdump($s);
$this->assertNotEmpty($s);
vdump($s);
$this->assertStringContainsString('on layout: template header;', $s);
$this->assertStringContainsString('on home: template body;', $s);
}

public function testEasy_useLayout02(): void
Expand All @@ -274,5 +276,7 @@ public function testEasy_useLayout02(): void
$t = $this->newTemplate();
$s = $t->render($tplFile, $this->tplVars);
$this->assertNotEmpty($s);
$this->assertStringContainsString('on layout: template header - name: inhere.', $s);
$this->assertStringContainsString('on home: template body - age: 20.', $s);
}
}
2 changes: 1 addition & 1 deletion test/testdata/easy/home01.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{ layout('layouts/layout01.tpl') }}

on home: block body;
on home: template body;
4 changes: 2 additions & 2 deletions test/testdata/easy/home02.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{ layout('layouts/layout01.tpl', ['info' => $info]) }}
{{ layout('layouts/layout02.tpl', ['info' => $info]) }}

on home: block body;
on home: template body - age: {{ info.age }}.
4 changes: 2 additions & 2 deletions test/testdata/easy/layouts/layout01.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
this is an layout file.

on layout: block header;
on layout: template header;

{{ contents() }}

on layout: block footer;
on layout: template footer;
4 changes: 2 additions & 2 deletions test/testdata/easy/layouts/layout02.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
this is an layout file.

on layout: block header - name: {{ info.name }}.
on layout: template header - name: {{ info.name }}.

{{ contents() }}

on layout: block footer - - name: {{ info.city }}.
on layout: template footer - city: {{ info.city }}.

0 comments on commit f4415b3

Please sign in to comment.