简单快速的 php 模板引擎。
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原生模板一样的,加入的特殊语法只是为了让使用更加方便。
use PhpPkg\EasyTpl\EasyTemplate;$t = EasyTemplate::new([ 'tplDir' => 'path/to/templates', 'allowExt' => ['.php', '.tpl'],]);// do something ...
更多设置:
立即学习“PHP免费学习笔记(深入)”;
/** @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 过滤器
可以使用 . 来快速访问数组值。原来的写法也是可用的,简洁写法也会自动转换为原生写法。
$arr = [ 'val0', 'subKey' => 'val1',];
在模板中使用:
first value is: {{ $arr.0 }} // val0'subKey' value is: {{ $arr.subKey }} // val1
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 }}
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
默认内置过滤器:
您可以在任何模板中使用过滤器。
基本使用:
{{ '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: github.com/phppkg/easytpl
以上就是详解PHP EasyTpl的功能及安装使用方法的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号