Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure output dir for Assetic in Symfony2

I'd like to globally configure the output dir of where assetic dumps my JS files. Currently, they always go to web/js/*. I want to change this to web/js/compiled/*.

It's possible to specify this at a per-file level: http://symfony.com/doc/2.0/cookbook/assetic/asset_management.html#dumping-asset-files

Can't seem to find a way to set this globally across my Symfony app. Any config parameter I'm missing?

UPDATE

Found an assetic config parameter called write_to. Setting this in config.yml causes the command line assetic:dump to dump files to the new dir, but within twig files the asset_url var still points to the original path.

like image 847
anushr Avatar asked Feb 23 '12 12:02

anushr


2 Answers

You should use the property write_to.

in my configuration for exemple I use

# Assetic Configuration
assetic:
    debug:          %kernel.debug%
    use_controller: %kernel.debug%
    read_from:      %kernel.root_dir%/Resources/views/
    write_to:       %kernel.root_dir%/../web/static/

Your ouput string start where ends write_to

for exemple

{% javascripts filter="closure" output='js/main.js'

...

 {% stylesheets filter='compass,?cssrewrite' 
     'default/static/sass/screen.scss' 
     output='css/screen.css' 
 %} 

both will placed respectively in /web/static/js/main.js and /web/static/css/screen.css

assets_base_urls is used to specify base URL's to be used for assets referenced from http and ssl (https) pages.

!! assets_base_urls is also used by {% images %} as the root before output value, but {% images %} doesn't consider write_to when rendering html (only when dumping) so better not using write_to and rely only on output value. More about it in my other post on stackoverflow and in this post on AsseticBundle's github.

like image 181
svassr Avatar answered Oct 13 '22 11:10

svassr


You can set the asset path ( assets_base_urls ) for twig to a static path, instead of using the relative path. In your config.yml file, it would look similar to this:

framework:
   templating:
      engines: ['twig']
         assets_base_urls:
            http: [http://path.to-cdn.com]

This will effect asset_url from assetic as well as twig's asset() method. The latter may or may not be desired.

like image 34
kmfk Avatar answered Oct 13 '22 10:10

kmfk