|
0
|
1 |
# Assetic  # |
|
|
2 |
|
|
|
3 |
Assetic is an asset management framework for PHP. |
|
|
4 |
|
|
|
5 |
``` php |
|
|
6 |
<?php |
|
|
7 |
|
|
|
8 |
use Assetic\Asset\AssetCollection; |
|
|
9 |
use Assetic\Asset\FileAsset; |
|
|
10 |
use Assetic\Asset\GlobAsset; |
|
|
11 |
|
|
|
12 |
$js = new AssetCollection(array( |
|
|
13 |
new GlobAsset('/path/to/js/*'), |
|
|
14 |
new FileAsset('/path/to/another.js'), |
|
|
15 |
)); |
|
|
16 |
|
|
|
17 |
// the code is merged when the asset is dumped |
|
|
18 |
echo $js->dump(); |
|
|
19 |
``` |
|
|
20 |
|
|
|
21 |
Assets |
|
|
22 |
------ |
|
|
23 |
|
|
|
24 |
An Assetic asset is something with filterable content that can be loaded and |
|
|
25 |
dumped. An asset also includes metadata, some of which can be manipulated and |
|
|
26 |
some of which is immutable. |
|
|
27 |
|
|
|
28 |
| **Property** | **Accessor** | **Mutator** | |
|
|
29 |
|--------------|-----------------|---------------| |
|
|
30 |
| content | getContent | setContent | |
|
|
31 |
| mtime | getLastModified | n/a | |
|
|
32 |
| source root | getSourceRoot | n/a | |
|
|
33 |
| source path | getSourcePath | n/a | |
|
|
34 |
| target path | getTargetPath | setTargetPath | |
|
|
35 |
|
|
|
36 |
Filters |
|
|
37 |
------- |
|
|
38 |
|
|
|
39 |
Filters can be applied to manipulate assets. |
|
|
40 |
|
|
|
41 |
``` php |
|
|
42 |
<?php |
|
|
43 |
|
|
|
44 |
use Assetic\Asset\AssetCollection; |
|
|
45 |
use Assetic\Asset\FileAsset; |
|
|
46 |
use Assetic\Asset\GlobAsset; |
|
|
47 |
use Assetic\Filter\LessFilter; |
|
|
48 |
use Assetic\Filter\Yui; |
|
|
49 |
|
|
|
50 |
$css = new AssetCollection(array( |
|
|
51 |
new FileAsset('/path/to/src/styles.less', array(new LessFilter())), |
|
|
52 |
new GlobAsset('/path/to/css/*'), |
|
|
53 |
), array( |
|
|
54 |
new Yui\CssCompressorFilter('/path/to/yuicompressor.jar'), |
|
|
55 |
)); |
|
|
56 |
|
|
|
57 |
// this will echo CSS compiled by LESS and compressed by YUI |
|
|
58 |
echo $css->dump(); |
|
|
59 |
``` |
|
|
60 |
|
|
|
61 |
The filters applied to the collection will cascade to each asset leaf if you |
|
|
62 |
iterate over it. |
|
|
63 |
|
|
|
64 |
``` php |
|
|
65 |
<?php |
|
|
66 |
|
|
|
67 |
foreach ($css as $leaf) { |
|
|
68 |
// each leaf is compressed by YUI |
|
|
69 |
echo $leaf->dump(); |
|
|
70 |
} |
|
|
71 |
``` |
|
|
72 |
|
|
|
73 |
The core provides the following filters in the `Assetic\Filter` namespace: |
|
|
74 |
|
|
|
75 |
* `CoffeeScriptFilter`: compiles CoffeeScript into Javascript |
|
|
76 |
* `CssEmbedFilter`: embeds image data in your stylesheets |
|
|
77 |
* `CssImportFilter`: inlines imported stylesheets |
|
|
78 |
* `CssMinFilter`: minifies CSS |
|
|
79 |
* `CssRewriteFilter`: fixes relative URLs in CSS assets when moving to a new URL |
|
|
80 |
* `GoogleClosure\CompilerApiFilter`: compiles Javascript using the Google Closure Compiler API |
|
|
81 |
* `GoogleClosure\CompilerJarFilter`: compiles Javascript using the Google Closure Compiler JAR |
|
|
82 |
* `JpegoptimFilter`: optimize your JPEGs |
|
|
83 |
* `JpegtranFilter`: optimize your JPEGs |
|
|
84 |
* `LessFilter`: parses LESS into CSS (using less.js with node.js) |
|
|
85 |
* `LessphpFilter`: parses LESS into CSS (using lessphp) |
|
|
86 |
* `OptiPngFilter`: optimize your PNGs |
|
|
87 |
* `PngoutFilter`: optimize your PNGs |
|
|
88 |
* `CompassFilter`: Compass CSS authoring framework |
|
|
89 |
* `Sass\SassFilter`: parses SASS into CSS |
|
|
90 |
* `Sass\ScssFilter`: parses SCSS into CSS |
|
|
91 |
* `SprocketsFilter`: Sprockets Javascript dependency management |
|
|
92 |
* `StylusFilter`: parses STYL into CSS |
|
|
93 |
* `Yui\CssCompressorFilter`: compresses CSS using the YUI compressor |
|
|
94 |
* `Yui\JsCompressorFilter`: compresses Javascript using the YUI compressor |
|
|
95 |
|
|
|
96 |
Asset Manager |
|
|
97 |
------------- |
|
|
98 |
|
|
|
99 |
An asset manager is provided for organizing assets. |
|
|
100 |
|
|
|
101 |
``` php |
|
|
102 |
<?php |
|
|
103 |
|
|
|
104 |
use Assetic\AssetManager; |
|
|
105 |
use Assetic\Asset\FileAsset; |
|
|
106 |
use Assetic\Asset\GlobAsset; |
|
|
107 |
|
|
|
108 |
$am = new AssetManager(); |
|
|
109 |
$am->set('jquery', new FileAsset('/path/to/jquery.js')); |
|
|
110 |
$am->set('base_css', new GlobAsset('/path/to/css/*')); |
|
|
111 |
``` |
|
|
112 |
|
|
|
113 |
The asset manager can also be used to reference assets to avoid duplication. |
|
|
114 |
|
|
|
115 |
``` php |
|
|
116 |
<?php |
|
|
117 |
|
|
|
118 |
use Assetic\Asset\AssetCollection; |
|
|
119 |
use Assetic\Asset\AssetReference; |
|
|
120 |
use Assetic\Asset\FileAsset; |
|
|
121 |
|
|
|
122 |
$am->set('my_plugin', new AssetCollection(array( |
|
|
123 |
new AssetReference($am, 'jquery'), |
|
|
124 |
new FileAsset('/path/to/jquery.plugin.js'), |
|
|
125 |
))); |
|
|
126 |
``` |
|
|
127 |
|
|
|
128 |
Filter Manager |
|
|
129 |
-------------- |
|
|
130 |
|
|
|
131 |
A filter manager is also provided for organizing filters. |
|
|
132 |
|
|
|
133 |
``` php |
|
|
134 |
<?php |
|
|
135 |
|
|
|
136 |
use Assetic\FilterManager; |
|
|
137 |
use Assetic\Filter\Sass\SassFilter; |
|
|
138 |
use Assetic\Filter\Yui; |
|
|
139 |
|
|
|
140 |
$fm = new FilterManager(); |
|
|
141 |
$fm->set('sass', new SassFilter('/path/to/parser/sass')); |
|
|
142 |
$fm->set('yui_css', new Yui\CssCompressorFilter('/path/to/yuicompressor.jar')); |
|
|
143 |
``` |
|
|
144 |
|
|
|
145 |
Asset Factory |
|
|
146 |
------------- |
|
|
147 |
|
|
|
148 |
If you'd rather not create all these objects by hand, you can use the asset |
|
|
149 |
factory, which will do most of the work for you. |
|
|
150 |
|
|
|
151 |
``` php |
|
|
152 |
<?php |
|
|
153 |
|
|
|
154 |
use Assetic\Factory\AssetFactory; |
|
|
155 |
|
|
|
156 |
$factory = new AssetFactory('/path/to/asset/directory/'); |
|
|
157 |
$factory->setAssetManager($am); |
|
|
158 |
$factory->setFilterManager($fm); |
|
|
159 |
$factory->setDebug(true); |
|
|
160 |
|
|
|
161 |
$css = $factory->createAsset(array( |
|
|
162 |
'@reset', // load the asset manager's "reset" asset |
|
|
163 |
'css/src/*.scss', // load every scss files from "/path/to/asset/directory/css/src/" |
|
|
164 |
), array( |
|
|
165 |
'scss', // filter through the filter manager's "scss" filter |
|
|
166 |
'?yui_css', // don't use this filter in debug mode |
|
|
167 |
)); |
|
|
168 |
|
|
|
169 |
echo $css->dump(); |
|
|
170 |
``` |
|
|
171 |
|
|
|
172 |
Prefixing a filter name with a question mark, as `yui_css` is here, will cause |
|
|
173 |
that filter to be omitted when the factory is in debug mode. |
|
|
174 |
|
|
|
175 |
Caching |
|
|
176 |
------- |
|
|
177 |
|
|
|
178 |
A simple caching mechanism is provided to avoid unnecessary work. |
|
|
179 |
|
|
|
180 |
``` php |
|
|
181 |
<?php |
|
|
182 |
|
|
|
183 |
use Assetic\Asset\AssetCache; |
|
|
184 |
use Assetic\Asset\FileAsset; |
|
|
185 |
use Assetic\Cache\FilesystemCache; |
|
|
186 |
use Assetic\Filter\Yui; |
|
|
187 |
|
|
|
188 |
$yui = new Yui\JsCompressorFilter('/path/to/yuicompressor.jar'); |
|
|
189 |
$js = new AssetCache( |
|
|
190 |
new FileAsset('/path/to/some.js', array($yui)), |
|
|
191 |
new FilesystemCache('/path/to/cache') |
|
|
192 |
); |
|
|
193 |
|
|
|
194 |
// the YUI compressor will only run on the first call |
|
|
195 |
$js->dump(); |
|
|
196 |
$js->dump(); |
|
|
197 |
$js->dump(); |
|
|
198 |
``` |
|
|
199 |
|
|
|
200 |
Static Assets |
|
|
201 |
------------- |
|
|
202 |
|
|
|
203 |
Alternatively you can just write filtered assets to your web directory and be |
|
|
204 |
done with it. |
|
|
205 |
|
|
|
206 |
``` php |
|
|
207 |
<?php |
|
|
208 |
|
|
|
209 |
use Assetic\AssetWriter; |
|
|
210 |
|
|
|
211 |
$writer = new AssetWriter('/path/to/web'); |
|
|
212 |
$writer->writeManagerAssets($am); |
|
|
213 |
``` |
|
|
214 |
|
|
|
215 |
Twig |
|
|
216 |
---- |
|
|
217 |
|
|
|
218 |
To use the Assetic [Twig][3] extension you must register it to your Twig |
|
|
219 |
environment: |
|
|
220 |
|
|
|
221 |
``` php |
|
|
222 |
<?php |
|
|
223 |
|
|
|
224 |
$twig->addExtension(new AsseticExtension($factory, $debug)); |
|
|
225 |
``` |
|
|
226 |
|
|
|
227 |
Once in place, the extension exposes a stylesheets and a javascripts tag with a syntax similar |
|
|
228 |
to what the asset factory uses: |
|
|
229 |
|
|
|
230 |
``` html+jinja |
|
|
231 |
{% stylesheets '/path/to/sass/main.sass' filter='sass,?yui_css' output='css/all.css' %} |
|
|
232 |
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" /> |
|
|
233 |
{% endstylesheets %} |
|
|
234 |
``` |
|
|
235 |
|
|
|
236 |
This example will render one `link` element on the page that includes a URL |
|
|
237 |
where the filtered asset can be found. |
|
|
238 |
|
|
|
239 |
When the extension is in debug mode, this same tag will render multiple `link` |
|
|
240 |
elements, one for each asset referenced by the `css/src/*.sass` glob. The |
|
|
241 |
specified filters will still be applied, unless they are marked as optional |
|
|
242 |
using the `?` prefix. |
|
|
243 |
|
|
|
244 |
This behavior can also be triggered by setting a `debug` attribute on the tag: |
|
|
245 |
|
|
|
246 |
``` html+jinja |
|
|
247 |
{% stylesheets 'css/*' debug=true %} ... {% stylesheets %} |
|
|
248 |
``` |
|
|
249 |
|
|
|
250 |
These assets need to be written to the web directory so these URLs don't |
|
|
251 |
return 404 errors. |
|
|
252 |
|
|
|
253 |
``` php |
|
|
254 |
<?php |
|
|
255 |
|
|
|
256 |
use Assetic\AssetWriter; |
|
|
257 |
use Assetic\Extension\Twig\TwigFormulaLoader; |
|
|
258 |
use Assetic\Extension\Twig\TwigResource; |
|
|
259 |
use Assetic\Factory\LazyAssetManager; |
|
|
260 |
|
|
|
261 |
$am = new LazyAssetManager($factory); |
|
|
262 |
|
|
|
263 |
// enable loading assets from twig templates |
|
|
264 |
$am->setLoader('twig', new TwigFormulaLoader($twig)); |
|
|
265 |
|
|
|
266 |
// loop through all your templates |
|
|
267 |
foreach ($templates as $template) { |
|
|
268 |
$resource = new TwigResource($twigLoader, $template); |
|
|
269 |
$am->addResource($resource, 'twig'); |
|
|
270 |
} |
|
|
271 |
|
|
|
272 |
$writer = new AssetWriter('/path/to/web'); |
|
|
273 |
$writer->writeManagerAssets($am); |
|
|
274 |
``` |
|
|
275 |
|
|
|
276 |
--- |
|
|
277 |
|
|
|
278 |
Assetic is based on the Python [webassets][1] library (available on |
|
|
279 |
[GitHub][2]). |
|
|
280 |
|
|
|
281 |
[1]: http://elsdoerfer.name/docs/webassets |
|
|
282 |
[2]: https://github.com/miracle2k/webassets |
|
|
283 |
[3]: http://www.twig-project.org |