EasyTpl - 简单快速的 PHP 模板引擎
inhere -EasyTpl - 简单快速的 PHP 模板引擎
⚡️ 简单快速的 PHP 模板引擎。
功能特性简单、轻量且快速。
无学习成本仅仅简单处理并转换为原生PHP语法兼容PHP原生语法使用更加简单的输出语法。 例如:{{= $var }}
{{ $var }}
{{ echo $var }}
支持所有控制语法。 例如 if,elseif,else;foreach;for;switch
支持链式访问数组值。 例如:{{ $arr.0 }}
{{ $map.name }}
{{ $map.user.name }}
更加安全,默认会自动通过 htmlspecialchars
将输出结果进行处理
raw
过滤器支持使用PHP内置函数作为过滤器。 例如:{{ $var | ucfirst }}
支持添加自定义过滤器
默认内置过滤器:upper
lower
nl
支持添加自定义指令,提供自定义功能支持模板中添加注释。 例如: {{# comments ... #}}
安装需要 PHP 8.0+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;
渲染输出:
My name is INHERE,
My develop tags:
- php
- go
- java
更多使用说明语法跟PHP原生模板一样的,加入的特殊语法只是为了让使用更加方便。
EasyTemplate
默认开启输出过滤,可用于渲染视图模板TextTemplate
则是关闭了输出过滤,主要用于文本处理,代码生成等配置设置use PhpPkg\EasyTpl\EasyTemplate;
$t = EasyTemplate::new([
'tplDir' => 'path/to/templates',
'allowExt' => ['.php', '.tpl'],
]);
// do something ...
更多设置:
/** @var PhpPkg\EasyTpl\EasyTemplate $t */
$t->disableEchoFilter();
$t->addFilter($name, $filterFn);
$t->addFilters([]);
$t->addDirective($name, $handler);
输出变量值下面的语句一样,都可以用于打印输出变量值
{{ $name }}
{{= $name }}
{{ echo $name }}
更多:
{{ $name ?: 'inhere' }}
{{ $age > 20 ? '20+' : '<= 20' }}
默认会自动通过 htmlspecialchars
将输出结果进行处理,除非设置了禁用或者手动使用 raw
过滤器设置禁用输出过滤 $t->disableEchoFilter()
模板中禁用输出过滤 {{ $name | raw }}
快速访问数组值可以使用 .
来快速访问数组值。原来的写法也是可用的,简洁写法也会自动转换为原生写法。
$arr = [
'val0',
'subKey' => 'val1',
];
在模板中使用:
first value is: {{ $arr.0 }} // val0
'subKey' value is: {{ $arr.subKey }} // val1
If 语句块if
语句:
{{ if ($name !== '') }}
hi, my name is {{ $name }}
{{ endif }}
if else
语句:
hi, my name is {{ $name }}
age is {{ $age }}, and
{{ if ($age >= 20) }}
age >= 20.
{{ else }}
age < 20.
{{ endif }}
if...elseif...else
语句:
hi, my name is {{ $name }}
age is {{ $age }}, and
{{ if ($age >= 50) }}
age >= 50.
{{ elseif ($age >= 20) }}
age >= 20.
{{ else }}
age < 20.
{{ endif }}
For/Foreach 语句块foreach
:
tags:
{{ foreach($tags as $tag) }}
- {{ $tag }}
{{ endforeach }}
with keys:
tags:
{{ foreach($tags as $index => $tag) }}
{{ $index }}. {{ $tag }}
{{ endforeach }}
模板中添加注释以 {{#
和 #}}
包裹的内容将会当做注释忽略。
{{# comments ... #}}{{ $name }} // inhere
multi lines:
{{#
this
comments
block
#}}{{ $name }} // inhere
使用过滤器默认内置过滤器:
upper
- 等同于 strtoupper
lower
- 等同于 strtolower
nl
- 追加换行符 \n
过滤器使用示例您可以在任何模板中使用过滤器。
基本使用:
{{ 'inhere' | ucfirst }} // Inhere
{{ 'inhere' | upper }} // INHERE
链式使用:
{{ 'inhere' | ucfirst | substr:0,2 }} // In
{{ '1999-12-31' | date:'Y/m/d' }} // 1999/12/31
传递非静态值:
{{ $name | ucfirst | substr:0,1 }}
{{ $user['name'] | ucfirst | substr:0,1 }}
{{ $userObj->name | ucfirst | substr:0,1 }}
{{ $userObj->getName() | ucfirst | substr:0,1 }}
将变量作为过滤器参数传递:
{{
$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: https://github.com/phppkg/eas...⚡️ 简单快速的 PHP 模板引擎。
功能特性简单、轻量且快速。
无学习成本仅仅简单处理并转换为原生PHP语法兼容PHP原生语法使用更加简单的输出语法。 例如:{{= $var }}
{{ $var }}
{{ echo $var }}
支持所有控制语法。 例如 if,elseif,else;foreach;for;switch
支持链式访问数组值。 例如:{{ $arr.0 }}
{{ $map.name }}
{{ $map.user.name }}
更加安全,默认会自动通过 htmlspecialchars
将输出结果进行处理
raw
过滤器支持使用PHP内置函数作为过滤器。 例如:{{ $var | ucfirst }}
支持添加自定义过滤器
默认内置过滤器:upper
lower
nl
支持添加自定义指令,提供自定义功能支持模板中添加注释。 例如: {{# comments ... #}}
安装需要 PHP 8.0+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;
渲染输出:
My name is INHERE,
My develop tags:
- php
- go
- java
更多使用说明语法跟PHP原生模板一样的,加入的特殊语法只是为了让使用更加方便。
EasyTemplate
默认开启输出过滤,可用于渲染视图模板TextTemplate
则是关闭了输出过滤,主要用于文本处理,代码生成等配置设置use PhpPkg\EasyTpl\EasyTemplate;
$t = EasyTemplate::new([
'tplDir' => 'path/to/templates',
'allowExt' => ['.php', '.tpl'],
]);
// do something ...
更多设置:
/** @var PhpPkg\EasyTpl\EasyTemplate $t */
$t->disableEchoFilter();
$t->addFilter($name, $filterFn);
$t->addFilters([]);
$t->addDirective($name, $handler);
输出变量值下面的语句一样,都可以用于打印输出变量值
{{ $name }}
{{= $name }}
{{ echo $name }}
更多:
{{ $name ?: 'inhere' }}
{{ $age > 20 ? '20+' : '<= 20' }}
默认会自动通过 htmlspecialchars
将输出结果进行处理,除非设置了禁用或者手动使用 raw
过滤器设置禁用输出过滤 $t->disableEchoFilter()
模板中禁用输出过滤 {{ $name | raw }}
快速访问数组值可以使用 .
来快速访问数组值。原来的写法也是可用的,简洁写法也会自动转换为原生写法。
$arr = [
'val0',
'subKey' => 'val1',
];
在模板中使用:
first value is: {{ $arr.0 }} // val0
'subKey' value is: {{ $arr.subKey }} // val1
If 语句块if
语句:
{{ if ($name !== '') }}
hi, my name is {{ $name }}
{{ endif }}
if else
语句:
hi, my name is {{ $name }}
age is {{ $age }}, and
{{ if ($age >= 20) }}
age >= 20.
{{ else }}
age < 20.
{{ endif }}
if...elseif...else
语句:
hi, my name is {{ $name }}
age is {{ $age }}, and
{{ if ($age >= 50) }}
age >= 50.
{{ elseif ($age >= 20) }}
age >= 20.
{{ else }}
age < 20.
{{ endif }}
For/Foreach 语句块foreach
:
tags:
{{ foreach($tags as $tag) }}
- {{ $tag }}
{{ endforeach }}
with keys:
tags:
{{ foreach($tags as $index => $tag) }}
{{ $index }}. {{ $tag }}
{{ endforeach }}
模板中添加注释以 {{#
和 #}}
包裹的内容将会当做注释忽略。
{{# comments ... #}}{{ $name }} // inhere
multi lines:
{{#
this
comments
block
#}}{{ $name }} // inhere
使用过滤器默认内置过滤器:
upper
- 等同于 strtoupper
lower
- 等同于 strtolower
nl
- 追加换行符 \n
过滤器使用示例您可以在任何模板中使用过滤器。
基本使用:
{{ 'inhere' | ucfirst }} // Inhere
{{ 'inhere' | upper }} // INHERE
链式使用:
{{ 'inhere' | ucfirst | substr:0,2 }} // In
{{ '1999-12-31' | date:'Y/m/d' }} // 1999/12/31
传递非静态值:
{{ $name | ucfirst | substr:0,1 }}
{{ $user['name'] | ucfirst | substr:0,1 }}
{{ $userObj->name | ucfirst | substr:0,1 }}
{{ $userObj->getName() | ucfirst | substr:0,1 }}
将变量作为过滤器参数传递:
{{
$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: https://github.com/phppkg/eas... php介绍
PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。PHP独特的语法混合了C、Java、Perl以及 PHP 自创的语法。利于学习,使用广泛,主要适用于Web开发领域。