Pate 是一个受 PHPTAL 启发的 PHP 属性模板引擎,但运行速度更快。它使用原生 DOM 来提高速度。

1.1.1 2017-03-21 07:39 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:30:37 UTC


README

Pate 是一个受 PHPTAL 启发的 PHP 属性模板引擎,但运行速度更快。它使用类似 angular 和 vue 的语法,但在服务器端渲染。它与 PHPTAL 兼容。

为什么选择 Pate 而不是 PHPTAL?

我在一个项目中使用了 PHPTAL 作为模板引擎。但它运行得太慢了,也太复杂了。所以我决定重写一个类似的模板引擎,名为 PATE

以下是它的优点

  • PATE 的速度比 PHPTAL 快 3 倍以上。PHPTAL 需要几秒钟来渲染一个大页面(大约 150k),但 PATE 更快。
  • PATE 极度可扩展。PATE 被设计为可扩展的,你可以创建自己的语法来增强 PATE 的功能。
  • PATE 极其简单。PATE 的代码量只有 PHPTAL 的十分之一,但功能更强大。
  • PATE 的编译文件非常易读。而 PHPTAL 的编译文件很复杂,很难读懂。
  • PHPTAL 对 DOM 和变量的要求过于严格,错误频繁发生。Pate 则更加友好。

要求

要使用 Pate,您需要

  • PHP >= 5.4。

  • 安装了 dom 扩展。

安装

使用 composer

composer require softwarezhu/pate

示例

属性

模板文件

<html>
<head>
</head>
<body>
<img tal:attributes="src data/src; alt data/title"/>
</body>
</html>

执行渲染(即,此模板文件名为 'template.html')

use Pate\PateTemplate;

$template = new PateTemplate();
$template->loadHtmlFile('template.html');
$content = $template->render(array(
    'data' => array(
        'src' => 'htttp://github.com/logo.jpg',
        'title' => 'Github Logo'
    )
));

echo $content;

渲染结果将是

<html>
    <head>
    </head>
    <body>
        <img src="htttp://github.com/logo.jpg" alt="Github Logo"/>
    </body>
</html>

模板语法

属性

 <img tal:attributes="src data/src; alt data/title"/>

img 的 srcalt 属性将被 data['src'] 的值和数据['title'] 的值替换。多个属性使用分号(;)分隔。

文本

 <span tal:content="data/title">This content will be replaced after rendered. </span>

循环

<tr tal:repeat="product products" tal:content="product/title">This content will be replaced by product/title after rendered. </tr>

如果产品的数量为 N,将会有 N 条 tr 行。

如果

 <div tal:condition="product/isPromote" tal:content="product/discount">If product/isPromote is false, this block will be removed from the dom. </div>

tal:condition 类似于 php 中的 if

替换

 <div tal:replace="123">This entire DIV will be replaced with '123'(not the inner text). </div>