liquid/liquid
Composer 安装命令:
composer require liquid/liquid
包简介
Liquid template engine for PHP
README 文档
README
Liquid is a PHP port of the Liquid template engine for Ruby, which was written by Tobias Lutke. Although there are many other templating engines for PHP, including Smarty (from which Liquid was partially inspired), Liquid had some advantages that made porting worthwhile:
- Readable and human friendly syntax, that is usable in any type of document, not just html, without need for escaping.
- Quick and easy to use and maintain.
- 100% secure, no possibility of embedding PHP code.
- Clean OO design, rather than the mix of OO and procedural found in other templating engines.
- Seperate compiling and rendering stages for improved performance.
- Easy to extend with your own "tags and filters":https://github.com/harrydeluxe/php-liquid/wiki/Liquid-for-programmers.
- 100% Markup compatibility with a Ruby templating engine, making templates usable for either.
- Unit tested: Liquid is fully unit-tested. The library is stable and ready to be used in large projects.
Why Liquid?
Why another templating library?
Liquid was written to meet three templating library requirements: good performance, easy to extend, and simply to use.
Installing
You can install this lib via composer:
composer require liquid/liquid
Example template
{% 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 %}
How to use Liquid
The main class is Liquid::Template class. There are two separate stages of working with Liquid templates: parsing and rendering. Here is a simple example:
use Liquid\Template;
$template = new Template();
$template->parse("Hello, {{ name }}!");
echo $template->render(array('name' => 'Alex'));
// Will echo
// Hello, Alex!
To find more examples have a look at the examples directory or at the original Ruby implementation repository's wiki page.
Advanced usage
You would probably want to add a caching layer (at very least a request-wide one), enable context-aware automatic escaping, and do load includes from disk with full file names.
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>',
]);
Will output:
Hello, Mx. Alex
<b>Your comment was:</b> <script>alert();</script>
Note that automatic escaping is not a standard Liquid feature: use with care.
Similarly, the following snippet will parse and render templates/home.liquid while storing parsing results in a class-local cache:
\Liquid\Liquid::set('INCLUDE_PREFIX', '');
$template = new \Liquid\Template(__DIR__ . '/protected/templates');
$template->setCache(new \Liquid\Cache\Local());
echo $template->parseFile('home')->render();
If you render the same template over and over for at least a dozen of times, the class-local cache will give you a slight speed up in range of some milliseconds per render depending on a complexity of your template.
You should probably extend Liquid\Template to initialize everything you do with Liquid::set in one place.
Custom filters
Adding filters has never been easier.
$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
Requirements
- PHP 8.2+
Some earlier versions could be used with PHP 5.x/7.x, though they're not supported anymore.
Issues
Have a bug? Please create an issue here on GitHub!
https://github.com/kalimatas/php-liquid/issues
Fork notes
This fork is based on php-liquid by Harald Hanek.
It contains several improvements:
- namespaces
- installing via composer
- new standard filters
rawtag added
Any help is appreciated!
liquid/liquid 适用场景与选型建议
liquid/liquid 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.15M 次下载、GitHub Stars 达 182, 最近一次更新时间为 2014 年 09 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「template」 「liquid」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 liquid/liquid 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 liquid/liquid 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 liquid/liquid 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Liquid template engine for Laravel
Liquid template engine for themes.
Liquid template engine for Laravel
Simple ASCII output of array data
Count words, get reading times, and convert text to list items.
E2E Studios PHP Framework adaptation of leafsphp/blade package
统计信息
- 总下载量: 5.15M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 184
- 点击次数: 33
- 依赖项目数: 24
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2014-09-07