Symfony2: A Few Assetic Notes

Just a quick post with a few notes on Assetic. These are mainly on topics people have been searching for when they have visited this blog.

Loading Filters

If you are using Assetic from Symfony2 then its worth being aware that it does not automatically load up all the available filters. Only the filters you specify in your app's config will be loaded. So for example to use the yui_css filter you need to specify it like this in the app's config.yml:

             jar: /home/miller/bin/yuicompressor-2.4.2.jar

It will then be loaded and available to use. If you do not do this you will get error messages such as:

InvalidArgumentException: There is no "yui_css" filter.

If the filter you want to use does not need options setting for it then you can make sure it is loaded like this:

        cssrewrite: ~

Watch for Changes to Dumped Files

Your assets can be dumped to the file systems as static files in order to serve them efficiently. For production you can do this on deploy. For development you can just let Assetic produce them on demand, however you can instead dump them as static files using the watch option like this:

app/console assetic:dump --watch

With this the command will not exit and loop constantly checking for changes to the files you are serving using Assetic, if any changes are detected they will be dumped again so you will see the changes. It will also respond to changes in the Twig templates, so if you do something such as include an additional CSS file in the collection of assets to be served as a single file then it will be dumped again including the extra asset.

Avoid Overwriting your Working Files

If you are dumping your files to web/css and web/js etc. then do not keep the working files there, they will get overwritten when you dump the files.

Image Resizing/Manipulation

You can use Assetic for some image processing, see my post Symfony2: Using Assetic for Image Optimisation for more details. However you cannot currently use it for tasks such as resizing and cropping. You can still do these tasks from Twig templates by using the AvalancheImagineBundle, which is a Symfony2 bundle to allow you to use the Imagine library from Twig. Imagine provides a standard Object Oriented interface for image manipulation in PHP and uses extensions such as GD2, Imagick and Gmagick for the processing. The bundle currently only has a built in filter for thumbnailing images but you can create custom filters for your own needs.