EasyTpl
Features
- It's simple, lightweight and fastly.
- No learning costs, syntax like PHP template
- 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 all control syntax. such as
if,elseif,else;foreach;for;switch
- support chained access array value. eg:
{{ $arr.0 }}
{{ $map.name }}
{{ $map.user.name }}
- More secure, the output will be processed automatically through
htmlspecialchars
by default- You can set to disable output filtering or manually use the
raw
filter
- You can set to disable output filtering or manually use the
- support php builtin function as filters. eg:
{{ $var | ucfirst }}
- support add custom filters.
- default builtin filters:
upper
lower
nl
- default builtin filters:
- support add custom directive.
- support comments in templates. eg:
{{# comments ... #}}
Install
- Required PHP 8.0+
composer
composer require phppkg/easytpl
Quick start
use PhpPkg\EasyTpl\EasyTemplate;
$tplCode = <<<'CODE'
My name is {{ $name | strtoupper }},
My develop tags:
{{ foreach($tags as $tag) }}
- {{ $tag }}
{{ endforeach }}
CODE;
$t = new EasyTemplate();
$str = $t->renderString($tplCode, [
'name' => 'inhere',
'tags' => ['php', 'go', 'java'],
]);
echo $str;
Output:
My name is INHERE,
My develop tags:
- php
- go
- java
More usage
config template
use PhpPkg\EasyTpl\EasyTemplate;
$t = EasyTemplate::new([
'tplDir' => 'path/to/templates',
'allowExt' => ['.php', '.tpl'],
]);
// do something ...
more settings:
/** @var PhpPkg\EasyTpl\EasyTemplate $t */
$t->disableEchoFilter();
$t->addFilter($name, $filterFn);
$t->addFilters([]);
$t->addDirective($name, $handler);
echo variable
The following statements are the same, can be used to print out variable values
{{ $name }}
{{= $name }}
{{ echo $name }}
By default, the output result will be automatically processed through
htmlspecialchars
, unless disabled or manually usedraw
filter
- Set to disable output filtering
$t->disableEchoFilter()
- Disable output filtering in the template
{{ $name | raw }}
chained access array
Can use .
to quick access array value.
$arr = [
'val0',
'subKey' => 'val1',
];
Use in template:
First value is: {{ $arr.0 }} // val0
'subKey' value is: {{ $arr.subKey }} // val1
add comments
{{# comments ... #}}{{ $name }} // inhere
multi lines:
{{#
this
comments
block
#}}{{ $name }} // inhere
Use Filters
Default built-in filters:
upper
- equalsstrtoupper
lower
- equalsstrtolower
nl
- append newline\n
Using the filters
You can use the filters in any of your templates.
Regular usage:
{{ 'inhere' | ucfirst }} // Inhere
{{ 'inhere' | upper }} // INHERE
Chained usage:
{{ 'inhere' | ucfirst | substr:0,2 }} // In
{{ '1999-12-31' | date:'Y/m/d' }} // 1999/12/31
Passing non-static values:
{{ $name | ucfirst | substr:0,1 }}
{{ $user['name'] | ucfirst | substr:0,1 }}
{{ $userObj->name | ucfirst | substr:0,1 }}
{{ $userObj->getName() | ucfirst | substr:0,1 }}
Passing variables as filter parameters:
{{
$suffix = '¥';
}}
{{ '12.75' | add_suffix:$suffix }} // 12.75¥
Custom filters
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);
},
]);
Use in template:
{{
$name = 'inhere';
}}
{{ $name | upper }} // INHERE
{{ $name | last3chars }} // ere
{{ $name | last3chars | upper }} // ERE
Custom directives
You can use the directives implement some special logic.
$tpl = EasyTemplate::new();
$tpl->addDirective(
'include',
function (string $body, string $name) {
/** will call {@see EasyTemplate::include()} */
return '$this->' . $name . $body;
}
);
Use in template:
{{ include('part/header.tpl', ['title' => 'My world']) }}