phpmob/twig-modify-bundle

Twig Minifier,你想要的全部都有。

安装次数: 5,441

依赖者: 2

建议者: 0

安全性: 0

星标: 2

关注者: 2

分支: 0

公开问题: 0

类型:symfony-bundle

2.0.0 2017-12-23 12:15 UTC

This package is auto-updated.

Last update: 2024-09-18 22:56:19 UTC


README

TwigModify是一个可以修改你所需一切的twig标签,例如cssminjsmin等。

安装

推荐通过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

许可协议

MIT