File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
How to Minify JavaScripts and Stylesheets with YUI Compressor
2+
=============================================================
3+
4+
Yahoo! provides an excellent utility for minifying JavaScripts and stylesheets
5+
so they travel over the wire faster, the `YUI Compressor`_. Thanks to Assetic,
6+
you can take advantage of this tool very easily.
7+
8+
Download the YUI Compressor JAR
9+
-------------------------------
10+
11+
The YUI Compressor is written in Java and distributed as a JAR. `Download the JAR`_
12+
from the Yahoo! site and save it to ``app/Resources/java/yuicompressor.jar``.
13+
14+
Configure the YUI Filters
15+
-------------------------
16+
17+
Now you need to configure two Assetic filters in your application, one for
18+
minifying JavaScripts with the YUI Compressor and one for minifying
19+
stylesheets:
20+
21+
.. configuration-block::
22+
23+
.. code-block:: yaml
24+
25+
# app/config/config.yml
26+
assetic:
27+
filters:
28+
yui_css:
29+
jar: "%kernel.root_dir%/Resources/java/yuicompressor.jar"
30+
yui_js:
31+
jar: "%kernel.root_dir%/Resources/java/yuicompressor.jar"
32+
33+
.. code-block:: xml
34+
35+
<!-- app/config/config.xml -->
36+
<assetic:config>
37+
<assetic:filter
38+
name="yui_css"
39+
jar="%kernel.root_dir%/Resources/java/yuicompressor.jar" />
40+
<assetic:filter
41+
name="yui_js"
42+
jar="%kernel.root_dir%/Resources/java/yuicompressor.jar" />
43+
</assetic:config>
44+
45+
.. code-block:: php
46+
47+
// app/config/config.php
48+
$container->loadFromExtension('assetic', array(
49+
'filters' => array(
50+
'yui_css' => array(
51+
'jar' => '%kernel.root_dir%/Resources/java/yuicompressor.jar',
52+
),
53+
'yui_js' => array(
54+
'jar' => '%kernel.root_dir%/Resources/java/yuicompressor.jar',
55+
),
56+
),
57+
));
58+
59+
You now have access to two new Assetic filters in your application:
60+
``yui_css`` and ``yui_js``. These will use the YUI Compressor to minify
61+
stylesheets and JavaScripts, respectively.
62+
63+
Minify your Assets
64+
------------------
65+
66+
You have YUI Compressor configured now, but nothing is going to happen until
67+
you apply one of these filters to an asset. Since your assets are a part of
68+
the view layer, this work is done in your templates:
69+
70+
.. configuration-block::
71+
72+
.. code-block:: html+jinja
73+
74+
{% javascripts 'js/src/*' filter='yui_js' %}
75+
<script src="{{ asset_url }}"></script>
76+
{% endjavascripts %}
77+
78+
.. code-block:: html+php
79+
80+
<?php foreach ($view['assetic']->javascripts(
81+
array('js/src/*'),
82+
array('yui_js')) as $url): ?>
83+
<script src="<?php echo $view->escape($url) ?>"></script>
84+
<?php endforeach; ?>
85+
86+
.. note::
87+
88+
The above example assumes that your JavaScript files are in the ``js/src``
89+
directory. This isn't important however - you can include your Javascript
90+
files no matter where they are.
91+
92+
With the addition of the ``yui_js`` filter to the asset tags above, you should
93+
now see minified JavaScripts coming over the wire much faster. The same process
94+
can be repeated to minify your stylesheets.
95+
96+
.. configuration-block::
97+
98+
.. code-block:: html+jinja
99+
100+
{% stylesheets 'css/src/*' filter='yui_css' %}
101+
<link rel="stylesheet" type="text/css" media="screen" href="{{ asset_url }}" />
102+
{% endjavascripts %}
103+
104+
.. code-block:: html+php
105+
106+
<?php foreach ($view['assetic']->stylesheets(
107+
array('css/src/*'),
108+
array('yui_css')) as $url): ?>
109+
<link rel="stylesheet" type="text/css" media="screen" href="<?php echo $view->escape($url) ?>" />
110+
<?php endforeach; ?>
111+
112+
Disable Minification in Debug Mode
113+
----------------------------------
114+
115+
Minified JavaScripts and Stylesheets are very difficult to read, let alone
116+
debug. Because of this, Assetic lets you disable a certain filter when your
117+
application is in debug mode. You can do this be prefixing the filter name
118+
in your template with a question mark: ``?``. This tells Assetic to only
119+
apply this filter when debug mode is off.
120+
121+
.. configuration-block::
122+
123+
.. code-block:: html+jinja
124+
125+
{% javascripts 'js/src/*' filter='?yui_js' %}
126+
<script src="{{ asset_url }}"></script>
127+
{% endjavascripts %}
128+
129+
.. code-block:: html+php
130+
131+
<?php foreach ($view['assetic']->javascripts(
132+
array('js/src/*'),
133+
array('?yui_js')) as $url): ?>
134+
<script src="<?php echo $view->escape($url) ?>"></script>
135+
<?php endforeach; ?>
136+
137+
.. _`YUI Compressor`: http://developer.yahoo.com/yui/compressor/
138+
.. _`Download the JAR`: http://yuilibrary.com/downloads/#yuicompressor

0 commit comments

Comments
 (0)