Where do Symfony2 shared CSS and JS assets go, best practice wise?

Viewed 24862

I normally place my JS and CSS Assetic assets inside a DefaultBundle, but I suppose the best place to put them would be in the app/Resources/public/(js|css) folders.

Also, to reference assets we use:

{% javascripts filter="" output="js/core.js" debug=true
    "@DefaultBundle/Resources/public/js/jquery-1.6.2.js" %}
    <script src="{{ asset_url }}" type="text/javascript"></script>
{% endjavascripts %}

How do I go about referencing shared assets like jQuery and my CSS reset stylesheets in app/Resources/public/...?

4 Answers

My solution is almost the same as David's, but using %kernel.root_dir% instead of hard-coding the app folder (although, I doubt many change this folder's name), e.g.:

{%- javascripts output='compiled/main.js'
    '%kernel.root_dir%/Resources/public/js/jquery-1.10.2.min.js'
    '%kernel.root_dir%/Resources/public/js/underscore-1.5.2.min.js'
    '@MyOwnBundle/Resources/public/js/default.js'
-%}
    <script src='{{ asset_url }}'></script>
{%- endjavascripts %}

If the third party libraries reference their own assets (e.g. typically images in a img sub-folder, referenced from .css files), it's usually sufficient to create the corresponding symlinks on the server, e.g. ln -s .../app/Resources/public/img .../web/img or something similar. Or use the cssrewrite filter, of course - but beware of its caveats.

(Also, note that I don't use the type="<mime-type>" attribute, as the files' content-type is supplied in the HTTP headers anyway.)

Related