How to Apply an Assetic Filter to a specific File Extension
Warning: You are browsing the documentation for Symfony 2.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.1 (the current stable version).
Caution
Starting from Symfony 2.8, Assetic is no longer included by default in the Symfony Standard Edition. Refer to this article to learn how to install and enable Assetic in your Symfony application.
Assetic filters can be applied to individual files, groups of files or even, as you'll see here, files that have a specific extension. To show you how to handle each option, suppose that you want to use Assetic's CoffeeScript filter, which compiles CoffeeScript files into JavaScript.
The main configuration is just the paths to coffee
, node
and node_modules
.
An example configuration might look like this:
1 2 3 4 5 6 7
# app/config/config.yml
assetic:
filters:
coffee:
bin: /usr/bin/coffee
node: /usr/bin/node
node_paths: [/usr/lib/node_modules/]
Filter a single File
You can now serve up a single CoffeeScript file as JavaScript from within your templates:
1 2 3
{% javascripts '@AppBundle/Resources/public/js/example.coffee' filter='coffee' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
This is all that's needed to compile this CoffeeScript file and serve it as the compiled JavaScript.
Filter multiple Files
You can also combine multiple CoffeeScript files into a single output file:
1 2 3 4 5
{% javascripts '@AppBundle/Resources/public/js/example.coffee'
'@AppBundle/Resources/public/js/another.coffee'
filter='coffee' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
Both files will now be served up as a single file compiled into regular JavaScript.
Filtering Based on a File Extension
One of the great advantages of using Assetic is reducing the number of asset files to lower HTTP requests. In order to make full use of this, it would be good to combine all your JavaScript and CoffeeScript files together since they will ultimately all be served as JavaScript. Unfortunately just adding the JavaScript files to the files to be combined as above will not work as the regular JavaScript files will not survive the CoffeeScript compilation.
This problem can be avoided by using the apply_to
option, which allows you
to specify which filter should always be applied to particular file extensions.
In this case you can specify that the coffee
filter is applied to all
.coffee
files:
1 2 3 4 5 6 7 8
# app/config/config.yml
assetic:
filters:
coffee:
bin: /usr/bin/coffee
node: /usr/bin/node
node_paths: [/usr/lib/node_modules/]
apply_to: '\.coffee$'
With this option, you no longer need to specify the coffee
filter in the
template. You can also list regular JavaScript files, all of which will be
combined and rendered as a single JavaScript file (with only the .coffee
files being run through the CoffeeScript filter):
1 2 3 4 5
{% javascripts '@AppBundle/Resources/public/js/example.coffee'
'@AppBundle/Resources/public/js/another.coffee'
'@AppBundle/Resources/public/js/regular.js' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}