phpmob / twig-modify-bundle
Twig Minifier,你想要的全部都有。
2.0.0
2017-12-23 12:15 UTC
Requires
- php: ^5.6|^7.0
- ezyang/htmlpurifier: ^4.9
- symfony/cache: ^3.1|~4.0
- symfony/config: ^3.1|~4.0
- symfony/dependency-injection: ^3.1|~4.0
- symfony/http-kernel: ^3.1|~4.0
- tedivm/jshrink: ^1.2
- tubalmartin/cssmin: ^4.1
- voku/anti-xss: ^4.0
Requires (Dev)
- phpunit/phpunit: ^6.4.3
README
TwigModify是一个可以修改你所需一切的twig标签,例如cssmin
、jsmin
等。
安装
推荐通过composer
进行安装。
"require": { "phpmob/twig-modify-bundle": "~2.0" }
启用
然后在AppKernel.php
中启用此扩展包
public function registerBundles() { $bundles = [ ... new \PhpMob\TwigModifyBundle\PhpMobTwigModifyBundle(), ]; }
用法
为了修改你的twig,只需简单地这样包裹{% modify modifier %}...{% modify %}
{% modify jsmin %} <script type="text/javascript"> $(function() { .... }); </script> {% endmodify %}
这就完成了!
内置修饰符
此扩展包支持以下修饰符。
JSMin
为\JShrink\Minifier::minify
提供的包裹修饰符,感谢tedivm/jshrink
{% modify jsmin %} <script type="text/javascript"> $(function() { .... }); </script> {% endmodify %}
CSSMin
为\tubalmartin\CssMin\Minifier::minify
提供的包裹修饰符,感谢tubalmartin/cssmin
{% modify cssmin %} <style type="text/css"> body { color: red; } </style> {% endmodify %}
HtmlPurify
为\HTMLPurifier::purify
提供的包裹修饰符,感谢ezyang/htmlpurifier
{% modify htmlpurify %} <SCRIPT » SRC=http://ha.ckers.org/xss. » js></SCRIPT> {% endmodify %}
AntiXss
为\voku\helper\AntiXSS
提供的包裹修饰符,感谢voku/anti-xss
{% modify antixss %} {{ harm_string|raw }} {% endmodify %}
缓存
默认情况下,TwigModifyBundle使用本地缓存文件夹,但你可以使用实现了\Symfony\Component\Cache\Adapter\AdapterInterface
接口的任何缓存,然后更改你的缓存服务的配置
phpmob_twig_modify: cache_adapter: "my_cache_adapter_service_id"
你也可以使用symfony 框架扩展包缓存适配器。
phpmob_twig_modify: cache_adapter: "cache.app"
别忘了在你的AppKernel.php
中启用DoctrineCacheBundle
- 请参阅https://symfony.com.cn/doc/current/bundles/DoctrineCacheBundle/index.html
自定义修饰符
你可以通过简单的配置添加/覆盖修饰符
phpmob_twig_modify: modifiers: xss: # a modifier name enabled: true # default true, toggle enable/disable this modifier. class: \PhpMob\TwigModifyBundle\Modifier\HTMLPurify # static class or any service method: purify # service method that's allowed to accept `[$content, (array) $options] options: # are array options you want to past into your modifier method - `\PhpMob\TwigModifyBundle\Modifier\HTMLPurify::purify` in this case. Cache.SerializerPath: "%kernel.cache_dir%/htmlpurify"
之后,你就可以使用你自己的新修饰符了。
{% modify xss %} <SCRIPT » SRC=http://ha.ckers.org/xss. » js></SCRIPT> {% endmodify %}
配置
配置参考
phpmob_twig_modify: # cache adapter serivce implemented `\Symfony\Component\Cache\Adapter\AdapterInterface` interface. cache_adapter: "my_cache_service_id" # application wide toggle enable/disable all modifiers enabled: true # custom/override modifiers (same key of modifier will override other previous defined) modifiers: name: class: ModifierClassOrService method: ModifierMethod options: ArrayOfSecondModifierMethod enabled: ToggleDisableEnable
感谢
- https://github.com/nibsirahsieu/SalvaJshrinkBundle to nice demonstration.
- https://github.com/Exercise/HTMLPurifierBundle to nice demonstration.
- https://github.com/ezyang/htmlpurifier
- https://github.com/tubalmartin/YUI-CSS-compressor-PHP-port
- https://github.com/tedious/JShrink
- https://github.com/voku/anti-xss