liquid / liquid
Liquid模板引擎,用于PHP
Requires
- php: ^7.4 || ^8.0
Requires (Dev)
- ergebnis/composer-normalize: >=2.8
- friendsofphp/php-cs-fixer: ^3.22
- infection/infection: >=0.17.6
- php-coveralls/php-coveralls: ^2.2
- phpunit/phpunit: ^9.2.6
- dev-master / 1.x-dev
- 1.4.42
- 1.4.41
- 1.4.40
- 1.4.39
- 1.4.38
- 1.4.37
- 1.4.36
- 1.4.35
- 1.4.34
- 1.4.33
- 1.4.32
- 1.4.31
- 1.4.30
- 1.4.29
- 1.4.28
- 1.4.27
- 1.4.26
- 1.4.25
- 1.4.24
- 1.4.23
- 1.4.22
- 1.4.21
- 1.4.20
- 1.4.19
- 1.4.18
- 1.4.17
- 1.4.16
- 1.4.15
- 1.4.14
- 1.4.13
- 1.4.12
- 1.4.11
- 1.4.10
- 1.4.9
- 1.4.8
- 1.4.7
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2
- 1.1
- 1.0
- dev-old
This package is auto-updated.
Last update: 2024-09-19 11:31:40 UTC
README
Liquid是Ruby的Liquid模板引擎(由Tobias Lutke编写)的PHP版本,尽管有其他许多PHP模板引擎,包括部分灵感来自Smarty的Liquid,但Liquid仍有一些优势,使得移植变得有意义。
- 可读性强、易于人类友好的语法,适用于任何类型的文档,而不仅仅是HTML,无需转义。
- 快速、易于使用和维护。
- 100%安全,没有嵌入PHP代码的可能性。
- 干净的OO设计,而不是其他模板引擎中常见的OO和过程式混合。
- 分离编译和渲染阶段,以提高性能。
- 易于扩展,添加自己的“标签和过滤器”:[https://github.com/harrydeluxe/php-liquid/wiki/Liquid-for-programmers](https://github.com/harrydeluxe/php-liquid/wiki/Liquid-for-programmers)。
- 100%与Ruby模板引擎兼容的标记,使得模板可用于两者。
- 单元测试:Liquid完全经过单元测试。该库稳定,可用于大型项目。
为什么选择Liquid?
为什么还需要另一个模板库?
Liquid是为了满足三个模板库要求而编写的:良好的性能、易于扩展和简单易用。
安装
您可以通过composer安装此库。
composer require liquid/liquid
示例模板
{% if products %}
<ul id="products">
{% for product in products %}
<li>
<h2>{{ product.name }}</h2>
Only {{ product.price | price }}
{{ product.description | prettyprint | paragraph }}
{{ 'it rocks!' | paragraph }}
</li>
{% endfor %}
</ul>
{% endif %}
如何使用Liquid
主要类是Liquid::Template
类。与Liquid模板一起工作的两个独立阶段是解析和渲染。以下是一个简单的例子
use Liquid\Template;
$template = new Template();
$template->parse("Hello, {{ name }}!");
echo $template->render(array('name' => 'Alex'));
// Will echo
// Hello, Alex!
要获取更多示例,请查看examples
目录或原始Ruby实现存储库的wiki页面。
高级使用
您可能希望添加一个缓存层(至少是请求范围的),启用上下文感知的自动转义,并使用完整文件名从磁盘加载包含文件。
use Liquid\Liquid;
use Liquid\Template;
use Liquid\Cache\Local;
Liquid::set('INCLUDE_SUFFIX', '');
Liquid::set('INCLUDE_PREFIX', '');
Liquid::set('INCLUDE_ALLOW_EXT', true);
Liquid::set('ESCAPE_BY_DEFAULT', true);
$template = new Template(__DIR__.'/protected/templates/');
$template->parse("Hello, {% include 'honorific.html' %}{{ plain-html | raw }} {{ comment-with-xss }}");
$template->setCache(new Local());
echo $template->render([
'name' => 'Alex',
'plain-html' => '<b>Your comment was:</b>',
'comment-with-xss' => '<script>alert();</script>',
]);
输出
Hello, Mx. Alex
<b>Your comment was:</b> <script>alert();</script>
请注意,自动转义不是Liquid的标准功能:请谨慎使用。
同样,以下代码片段将解析和渲染templates/home.liquid
,同时将解析结果存储在类局部缓存中
\Liquid\Liquid::set('INCLUDE_PREFIX', '');
$template = new \Liquid\Template(__DIR__ . '/protected/templates');
$template->setCache(new \Liquid\Cache\Local());
echo $template->parseFile('home')->render();
如果您多次渲染相同的模板,至少12次,类局部缓存将使您在每次渲染中略微提高一些毫秒的速度,具体取决于模板的复杂性。
您可能希望扩展Liquid\Template
以在Liquid::set
的一个位置初始化您所做的所有操作。
自定义过滤器
添加过滤器从未如此简单。
$template = new Template();
$template->registerFilter('absolute_url', function ($arg) {
return "https://www.example.com$arg";
});
$template->parse("{{ my_url | absolute_url }}");
echo $template->render(array(
'my_url' => '/test'
));
// expect: https://www.example.com/test
要求
- PHP 7.0+
某些早期版本可用于PHP 5.3/5.4/5.5/5.6,但不再受支持。
问题
如果您有错误,请在这里GitHub上创建一个问题!
https://github.com/kalimatas/php-liquid/issues
分支说明
此分支基于Harald Hanek的php-liquid。
它包含一些改进
- 命名空间
- 通过composer安装
- 新标准过滤器
raw
标签添加
任何帮助都将受到赞赏!