# Assetic ![project status](http://stillmaintained.com/kriswallsmith/assetic.png) # Assetic is an asset management framework for PHP. ``` php dump(); ``` Assets ------ An Assetic asset is something with filterable content that can be loaded and dumped. An asset also includes metadata, some of which can be manipulated and some of which is immutable. | **Property** | **Accessor** | **Mutator** | |--------------|-----------------|---------------| | content | getContent | setContent | | mtime | getLastModified | n/a | | source root | getSourceRoot | n/a | | source path | getSourcePath | n/a | | target path | getTargetPath | setTargetPath | Filters ------- Filters can be applied to manipulate assets. ``` php dump(); ``` The filters applied to the collection will cascade to each asset leaf if you iterate over it. ``` php dump(); } ``` The core provides the following filters in the `Assetic\Filter` namespace: * `CoffeeScriptFilter`: compiles CoffeeScript into Javascript * `CssEmbedFilter`: embeds image data in your stylesheets * `CssImportFilter`: inlines imported stylesheets * `CssMinFilter`: minifies CSS * `CssRewriteFilter`: fixes relative URLs in CSS assets when moving to a new URL * `GoogleClosure\CompilerApiFilter`: compiles Javascript using the Google Closure Compiler API * `GoogleClosure\CompilerJarFilter`: compiles Javascript using the Google Closure Compiler JAR * `JpegoptimFilter`: optimize your JPEGs * `JpegtranFilter`: optimize your JPEGs * `LessFilter`: parses LESS into CSS (using less.js with node.js) * `LessphpFilter`: parses LESS into CSS (using lessphp) * `OptiPngFilter`: optimize your PNGs * `PackerFilter`: compresses Javascript using Dean Edwards's Packer * `PngoutFilter`: optimize your PNGs * `CompassFilter`: Compass CSS authoring framework * `Sass\SassFilter`: parses SASS into CSS * `Sass\ScssFilter`: parses SCSS into CSS * `SprocketsFilter`: Sprockets Javascript dependency management * `StylusFilter`: parses STYL into CSS * `Yui\CssCompressorFilter`: compresses CSS using the YUI compressor * `Yui\JsCompressorFilter`: compresses Javascript using the YUI compressor Asset Manager ------------- An asset manager is provided for organizing assets. ``` php set('jquery', new FileAsset('/path/to/jquery.js')); $am->set('base_css', new GlobAsset('/path/to/css/*')); ``` The asset manager can also be used to reference assets to avoid duplication. ``` php set('my_plugin', new AssetCollection(array( new AssetReference($am, 'jquery'), new FileAsset('/path/to/jquery.plugin.js'), ))); ``` Filter Manager -------------- A filter manager is also provided for organizing filters. ``` php set('sass', new SassFilter('/path/to/parser/sass')); $fm->set('yui_css', new Yui\CssCompressorFilter('/path/to/yuicompressor.jar')); ``` Asset Factory ------------- If you'd rather not create all these objects by hand, you can use the asset factory, which will do most of the work for you. ``` php setAssetManager($am); $factory->setFilterManager($fm); $factory->setDebug(true); $css = $factory->createAsset(array( '@reset', // load the asset manager's "reset" asset 'css/src/*.scss', // load every scss files from "/path/to/asset/directory/css/src/" ), array( 'scss', // filter through the filter manager's "scss" filter '?yui_css', // don't use this filter in debug mode )); echo $css->dump(); ``` Prefixing a filter name with a question mark, as `yui_css` is here, will cause that filter to be omitted when the factory is in debug mode. Caching ------- A simple caching mechanism is provided to avoid unnecessary work. ``` php dump(); $js->dump(); $js->dump(); ``` Static Assets ------------- Alternatively you can just write filtered assets to your web directory and be done with it. ``` php writeManagerAssets($am); ``` Twig ---- To use the Assetic [Twig][3] extension you must register it to your Twig environment: ``` php addExtension(new AsseticExtension($factory, $debug)); ``` Once in place, the extension exposes a stylesheets and a javascripts tag with a syntax similar to what the asset factory uses: ``` html+jinja {% stylesheets '/path/to/sass/main.sass' filter='sass,?yui_css' output='css/all.css' %} {% endstylesheets %} ``` This example will render one `link` element on the page that includes a URL where the filtered asset can be found. When the extension is in debug mode, this same tag will render multiple `link` elements, one for each asset referenced by the `css/src/*.sass` glob. The specified filters will still be applied, unless they are marked as optional using the `?` prefix. This behavior can also be triggered by setting a `debug` attribute on the tag: ``` html+jinja {% stylesheets 'css/*' debug=true %} ... {% stylesheets %} ``` These assets need to be written to the web directory so these URLs don't return 404 errors. ``` php setLoader('twig', new TwigFormulaLoader($twig)); // loop through all your templates foreach ($templates as $template) { $resource = new TwigResource($twigLoader, $template); $am->addResource($resource, 'twig'); } $writer = new AssetWriter('/path/to/web'); $writer->writeManagerAssets($am); ``` --- Assetic is based on the Python [webassets][1] library (available on [GitHub][2]). [1]: http://elsdoerfer.name/docs/webassets [2]: https://github.com/miracle2k/webassets [3]: http://twig.sensiolabs.org