Skip to content
RWOverdijk edited this page Jan 31, 2013 · 11 revisions

Filters can be very important. When using lesscss, large unminified javascript libraries like jquery or even just collections of unminified javascript code you wrote you might want to apply filters before outputting, or caching filters. Making use of assetic you already have access to a lot of standard filters.


Missing filters.

I've had complaints about these filters not working out of the box. This is because the assetic filters supply adapters to use the actual filters. So in order to use these filters, you'll have to supply the dependencies yourself. A good example (thanks to Ocramius) is Zend\Cache. This supplies adapters for Memcache, Apc etc. It assumes that you have those dependencies installed (or in our case included) already. The same happens with assetic filters.

Filtering assets

Filtering assets has a lot of benefits. It allows you to minify code, optimize images, use dynamic stylesheet languages, use stuff like coffeescript and so on. AssetManager applies the filters to specific assets. This can be useful, especially when combining assets in asset collections.

How to apply filters

Applying filters is pretty straight forward. You use the same method that's used when applying caching. Enough talking, here's are some examples.

By alias

You can apply filters to specific aliases (assets, asset collections or resolved assets in paths).

<?php
return array(
    'asset_manager' => array(
        'filters' => array(
            'js/myAsset.js' => array(
                array(
                    // Note: You will need to require the classes used for the filters yourself.
                    'filter' => 'JSMin',  // Allowed format is Filtername[Filter]. Can also be FQCN
                ),
            ),
        ),
    ),
);

By mime type

If you wish to apply filters to assets by mime type, this is possible as of version 1.1.0.

<?php
return array(
    'asset_manager' => array(
        'filters' => array(
            'application/javascript' => array(
                array(
                    // Note: You will need to require the classes used for the filters yourself.
                    'filter' => 'JSMin',  // Allowed format is Filtername[Filter]. Can also be FQCN
                ),
            ),
        ),
    ),
);

By extension

If you wish to apply filters to assets by file extension, this is possible as of version 1.1.0.

<?php
return array(
    'asset_manager' => array(
        'filters' => array(
            'js' => array(
                array(
                    // Note: You will need to require the classes used for the filters yourself.
                    'filter' => 'JSMin',  // Allowed format is Filtername[Filter]. Can also be FQCN
                ),
            ),
        ),
    ),
);

Services

Alternatively, it's possible to apply filters using services. This can be useful if you feel like adding arguments (for example with the LessFilter).

Here's an example:

<?php
return array(
    'asset_manager' => array(
        'filters' => array(
            'css/bootstrap.css' => array(
                array(
                    'service' => 'my_awesome_filter',
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'my_awesome_filter' => function($sm) {
                return new \Assetic\Filter\LessFilter(
                    '/home/rwoverdijk/bin/node',
                    array('/home/rwoverdijk/bin/node_modules')
                );
            },
        ),
    ),
);

That's it

There's not much to it. This is all there is to it. You can build your own filters implementing the Assetic\Filter\FilterInterface interface, but I don't think it'll come to that because of the large amount of filters already supplied by assetic.

Clone this wiki locally