1
- How to Apply an Assetic Filter to a Specified File Extension
2
- ============================================================
1
+ How to Apply an Assetic Filter to a Specific File Extension
2
+ ===========================================================
3
3
4
- One of Assetic's filters compiles CoffeeScript files into JavaScript.
5
- This offers the advantages of caching the end result so the compilation
6
- does not run every time whilst also automatically recompiling when ever
7
- the script changes during development.
8
-
9
- Whilst Assetic's ``CoffeeScriptFilter `` will do the work for you it does
10
- not do the actual compilation, you will still need to install CoffeeScript
11
- itself along with node.js on which it runs.
4
+ Assetic filters can be applied to individual files, groups of files or even,
5
+ as you'll see here, files that have a specific extension. To show you how
6
+ to handle each option, let's suppose that you want to use Assetic's CoffeeScript
7
+ filter, which compiles CoffeeScript files into Javascript.
12
8
13
9
The main configuration is just the paths to coffee and node. These default
14
10
respectively to ``/usr/bin/coffee `` and ``/usr/bin/node ``:
@@ -46,8 +42,10 @@ respectively to ``/usr/bin/coffee`` and ``/usr/bin/node``:
46
42
),
47
43
));
48
44
45
+ Filter a Single File
46
+ --------------------
49
47
50
- You can then serve up CoffeeScript files as JavaScript from within your
48
+ You can now serve up a single CoffeeScript file as JavaScript from within your
51
49
templates:
52
50
53
51
.. configuration-block ::
@@ -68,9 +66,13 @@ templates:
68
66
<script src="<?php echo $view->escape($url) ?>" type="text/javascript"></script>
69
67
<?php endforeach; ?>
70
68
69
+ This is all that's needed to compile this CoffeeScript file and server it
70
+ as the compiled JavaScript.
71
+
72
+ Filter Multiple Files
73
+ ---------------------
71
74
72
- This is all was needed and the file was served up as regular JavaScript.
73
- You can combine multiple CoffeeScript files into a single output file:
75
+ You can also combine multiple CoffeeScript files into a single output file:
74
76
75
77
.. configuration-block ::
76
78
@@ -92,19 +94,21 @@ You can combine multiple CoffeeScript files into a single output file:
92
94
<script src="<?php echo $view->escape($url) ?>" type="text/javascript"></script>
93
95
<?php endforeach; ?>
94
96
95
-
96
97
Both the files will now be served up as a single file compiled into regular
97
98
JavaScript.
98
99
100
+ Filtering based on a File Extension
101
+ -----------------------------------
102
+
99
103
One of the great advantages of using Assetic is reducing the number of asset
100
- files to lower `` HTTP `` requests. In order to make full use of this it would
101
- be good to combine all your JavaScript and CoffeeScript files together
104
+ files to lower HTTP requests. In order to make full use of this, it would
105
+ be good to combine * all * your JavaScript and CoffeeScript files together
102
106
since they will ultimately all be served as JavaScript. Unfortunately just
103
107
adding the JavaScript files to the files to be combined as above will not
104
108
work as the regular JavaScript files will not survive the CoffeeScript compilation.
105
109
106
- This problem can be avoid though by using the ``apply_to `` option in the
107
- config, this allows you to specify that a filter is always applied to particular
110
+ This problem can be avoided by using the ``apply_to `` option in the config,
111
+ which allows you to specify that a filter should always be applied to particular
108
112
file extensions. In this case you can specify that the Coffee filter is
109
113
applied to all ``.coffee `` files:
110
114
@@ -144,11 +148,10 @@ applied to all ``.coffee`` files:
144
148
),
145
149
));
146
150
147
-
148
-
149
- So you can now remove specifying the filter in the Twig template and list
150
- any regular JavaScript files which will now be combined into the output
151
- file without having been run through coffee:
151
+ With this, you no longer need to specify the ``coffee `` filter in the template.
152
+ You can also list regular JavaScript files, all of which will be combined
153
+ and rendered as a single JavaScript file (with only the ``.coffee `` files
154
+ being run through the CoffeeScript filter):
152
155
153
156
.. configuration-block ::
154
157
0 commit comments