phpmob/twig-modify-bundle

Twig Minifier just you like.

Installs: 5 433

Dependents: 2

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 0

Open Issues: 0

Type:symfony-bundle

2.0.0 2017-12-23 12:15 UTC

This package is auto-updated.

Last update: 2024-04-18 21:58:15 UTC


README

TwigModify is a twig tag that can modifies everythink you want eg. cssmin, jsmin and more.

Installing

Installing via composer is recommended.

"require": {
  "phpmob/twig-modify-bundle": "~2.0"
}

Enabling

And then enable bundle in AppKernel.php

public function registerBundles()
{
    $bundles = [
        ...
        new \PhpMob\TwigModifyBundle\PhpMobTwigModifyBundle(),
    ];
}

Usage

In order to modify your twig is simple just wrap {% modify modifier %}...{% modify %} like this:

{% modify jsmin %}
    <script type="text/javascript">
        $(function() {
                ....
        });
    </script>
{% endmodify %}

That was it!

Build-in Modifiers

There are supported modifiers in this bundle.

JSMin

A wrapped modifier for \JShrink\Minifier::minify, Thanks tedivm/jshrink

{% modify jsmin %}
    <script type="text/javascript">
        $(function() {
                ....
        });
    </script>
{% endmodify %}

CSSMin

A wrapped modifier for \tubalmartin\CssMin\Minifier::minify, Thanks tubalmartin/cssmin

{% modify cssmin %}
    <style type="text/css">
        body {
            color: red;
        }
    </style>
{% endmodify %}

HtmlPurify

A wrapped modifier for \HTMLPurifier::purify, Thanks ezyang/htmlpurifier

{% modify htmlpurify %}
    <SCRIPT »
    SRC=http://ha.ckers.org/xss. »
    js></SCRIPT>
{% endmodify %}

AntiXss

A wrapped modifier for \voku\helper\AntiXSS, Thanks voku/anti-xss

{% modify antixss %}
    {{ harm_string|raw }}
{% endmodify %}

Cache

TwigModifyBundle use local cache folder by default, however you can use any cache that implemented \Symfony\Component\Cache\Adapter\AdapterInterface interface and then change the confiuration for your cache service:

phpmob_twig_modify:
    cache_adapter: "my_cache_adapter_service_id"

You can also use symfony framework-bundle cache adapter.

phpmob_twig_modify:
    cache_adapter: "cache.app"

Don't fogot enable DoctrineCacheBundle in your AppKernel.php - See https://symfony.com/doc/current/bundles/DoctrineCacheBundle/index.html

Your own modifiers

You can add/override modifiers by easy configuration:

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"

After that you already to use your new modifier.

 {% modify xss %}
     <SCRIPT »
     SRC=http://ha.ckers.org/xss. »
     js></SCRIPT>
 {% endmodify %}

Configuration

Configuration reference:

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

Thanks

- 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

License

MIT