400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

PHP中EasyTpl如何安装使用

这篇文章主要为大家展示了“PHP中EasyTpl如何安装使用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“PHP中EasyTpl如何安装使用”这篇文章吧。

创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都网站建设、网站设计、月湖网络推广、微信小程序定制开发、月湖网络营销、月湖企业策划、月湖品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供月湖建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

功能特性

安装

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 ...

更多设置:

/** @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 语句:

{{ 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

使用过滤器

默认内置过滤器:

过滤器使用示例

您可以在任何模板中使用过滤器。

基本使用:

{{ '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']) }}

以上是“PHP中EasyTpl如何安装使用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


网页名称:PHP中EasyTpl如何安装使用
浏览地址:http://mbwzsj.com/article/gjoghi.html

其他资讯

让你的专属顾问为你服务