tigr/twig-preprocessor
最新稳定版本:3.0.0
Composer 安装命令:
composer require tigr/twig-preprocessor
包简介
This very simple loader adds a way to pre parse twig files before loading them into twig parser
README 文档
README
This Twig Preprocessor loader allows you to do custom manipulations with twig templates before passing them to twig parser. This allows you, for instance, to do some substitutions, or format templates to make them look better.
This branch (master) contains code for Twig 2.x.
Installation
Installation via composer (version for twig 2):
composer require tigr/twig-preprocessor
If you want to use it with twig 1, use version 1:
composer require tigr/twig-preprocessor "~1.0"
Usage
In general usage, you pass real template loader to Twig Preprocessor Loader and also a callback that would be called to mingle with twig template code. So, general usage is this:
$realLoader = Twig_Loader_Filesystem('/path/to/templates'); $loader = Twig_Loader_Preprocessor( $realLoader, function($twigSource) { // do something with $twigSource return $twigSource; } ); $twig = new Twig_Environment($loader);
The main reason why this code was written was to make Twig output a bit more pretty-formatted code. You can read more about this in this Twig issue.
In short, the idea is to remove all spaces/tabs before any (well, most) twig control structures. Here is how you can achieve this:
$loader = new Twig_Loader_Preprocessor( $realLoader, function ($template) { static $regExp; // the RE isn't perfect, it won't match structures having curly braces within, // but it's okay for me. if (!isset($regExp)) { $regExp = str_replace( ['_', '{', '}'], ['[\t ]*', '\{', '\}'], '/^_({([#%])[^}]*[^-](?2)}|{%_block_\w*_%}{%_endblock_%})$/m' ); } return preg_replace($regExp, '$1', $template); } );
History
统计信息
- 总下载量: 39.95k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-01-09