Configure output dir for Assetic in Symfony2
Asked Answered
M

5

14

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.

Monogenetic answered 23/2, 2012 at 12:19 Comment(4)
did you ever come up with a solution? I would have assumed that the read_from option in the config would be what we're looking. However, I can't get that option to do anything. Regardless of what I set the read_from parameter to, my application uses the default path.Trichinopoly
@Trichinopoly Nothing yet. Right now, we're just setting the output param for each javascripts block, but that's not at all ideal.Monogenetic
Yeah, I have assetic dump my assets straight to our S3 bucket and I wanted the prod environment to read files from there - while the dev environment dumps to a local folder and like-wise, my templates should read from that folder while on our dev server. I ended up checking the environment in the controller and passing a var to the template to prefix the asset_url for the cdn. Not ideal, but it does the job.Trichinopoly
So why exists this "write_to" parameter, when the variable {{ asset_url }} does not heed to it. That is not thought through at all... :(Wien
C
17

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.

Casseycassi answered 2/5, 2012 at 19:24 Comment(0)
T
4

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.

Trichinopoly answered 11/4, 2012 at 21:2 Comment(0)
B
2

This GitHub issue comment helped me with this issue. While in dev, your assets will still go thru the controller but in production, the URLs will be as you desire.

Example config.yml:

assetic:
    write-to:  %kernel.root_dir%/../web/assets
    ...
framework:
    ...
    templating:
        engines: ['twig']
        packages:
            assetic:
                base_urls: '/assets'

Example in your template:

{% block javascripts %}
    {% javascripts '@jquery' '@bootstrap_js' '@backbone' '@handlebars' combine=true package='assetic' %}
    <script src="{{ asset_url }}"></script>
    {% endjavascripts %}
{% endblock %}

Notice that you have to add the package='assetic' attribute in the java scripts tag. This is a good compromise IMO because it won't break assets from other bundles as kmfk's solution will.

Bisect answered 5/9, 2013 at 15:19 Comment(0)
C
2

Just a quick note on this. If you're using assets_base_urls, to specify a relative base URL, this only works prior to Symfony 2.7, due to the introduction of the new assets component in that version. Further information on how to change this is available at http://symfony.com/blog/new-in-symfony-2-7-the-new-asset-component , but the long and short of it is that:

framework:
    templating:
        assets_base_urls: 
            http: ['/some-relative-url']
            ssl: ['/some-relative-url']

becomes:

framework:
    assets:
        base_path: /some-relative-url
Curtal answered 27/1, 2016 at 16:46 Comment(0)
E
-4

Try this commande $ app/console --env=prod assetic:dump web/ you have juste to change the url you want raher than 'web/'

Encage answered 29/7, 2013 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.