Simple and fast PHP template engine.
{{= $var }}
{{ $var }}
{{ echo $var }}
if,elseif,else;foreach;for;switch
{{ $arr.0 }}
{{ $map.name }}
{{ $map.user.name }}
htmlspecialchars
by defaultraw
filter{{ $var | ucfirst }}
upper
lower
nl
{{# comments ... #}}
composer
composer require phppkg/easytpl
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;
Rendering Output:
My name is INHERE,My develop tags:- php- go- java
The syntax is the same as the PHP native template. The special syntax is added just to make it more convenient to use.
EasyTemplate
Output filtering is turned on by default and can be used to render view templates TextTemplate
Turns off output filtering and is mainly used for text Processing, code generation, etc.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);
below The same statements can be used to print output variable values
{{ $name }}{{= $name }}{{ echo $name }}
More:
{{ $name ?: 'inhere' }}{{ $age > 20 ? '20+' : '<= 20' }}
By default, the output results will be automatically processed through
htmlspecialchars
, unless Set to disable or manually use theraw
filter
$t->disableEchoFilter()
{{ $name | raw }}
. to quickly access array values. The original writing method is also available, and the concise writing method will be automatically converted to the native writing method.
$arr = [ 'val0', 'subKey' => 'val1',];
first value is: {{ $arr.0 }} // val0'subKey' value is: {{ $arr.subKey }} // val1
if Statement:
{{ if ($name !== '') }}hi, my name is {{ $name }}{{ endif }}
if else Statement:
hi, my name is {{ $name }}age is {{ $age }}, and{{ if ($age >= 20) }} age >= 20.{{ else }} age < 20.{{ endif }}
if...elseif...else Statement:
hi, my name is {{ $name }}age is {{ $age }}, and{{ if ($age >= 50) }} age >= 50.{{ elseif ($age >= 20) }} age >= 20.{{ else }} age < 20.{{ endif }}
foreach:
tags:{{ foreach($tags as $tag) }}- {{ $tag }}{{ endforeach }}
tags:{{ foreach($tags as $index => $tag) }}{{ $index }}. {{ $tag }}{{ endforeach }}
{{# and
#}} The contents of the package will be ignored as comments.
{{# comments ... #}}{{ $name }} // inhere
{{# this comments block #}}{{ $name }} // inhere
- Equivalent to
strtoupper
- Equivalent to
strtolower
- Append newline
\n
Basic usage:
{{ 'inhere' | ucfirst }} // Inhere {{ 'inhere' | upper }} // INHERE
Chain 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 }}
Pass variables as filter parameters:
{{ $suffix = '¥';}}{{ '12.75' | add_suffix:$suffix }} // 12.75¥
use PhpPkg\EasyTpl\EasyTemplate;$tpl = EasyTemplate::new();// use php built function$tpl->addFilter('upper', 'strtoupper');// 一次添加多个$tpl->addFilters([ 'last3chars' => function (string $str): string { return substr($str, -3); },]);
{{ $name = 'inhere';}}{{ $name | upper }} // INHERE{{ $name | last3chars }} // ere{{ $name | last3chars | upper }} // ERE
$tpl = EasyTemplate::new();$tpl->addDirective( 'include', function (string $body, string $name) { /** will call {@see EasyTemplate::include()} */ return '$this->' . $name . $body; });
{{ include('part/header.tpl', ['title' => 'My world']) }}
Github: github.com/phppkg/easytpl
The above is the detailed content of Detailed explanation of the functions and installation and use of PHP EasyTpl. For more information, please follow other related articles on the PHP Chinese website!