开发者

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

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 %}
开发者_如何学Go

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


You could also use this:

{% stylesheets '../app/Resources/public/css/*' %}
    <link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
{% endstylesheets %}


Yes it's a good choice to not put libraries inside a Bundle

see here : http://symfony.com/doc/current/cookbook/bundles/best_practices.html#vendors

A bundle should not embed third-party libraries written in JavaScript, CSS, or any other language.


What i suggest you, is to put you jquery and reset files under something like: app/Resources/public/js/jquery.min.js and to change your code with something like:

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

I hope this will work for you.

EDIT: Edited answer to be like https://stackoverflow.com/a/9237004/505836, thanks David.


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.)


You can replace ../app/Resources/public/css/ by public/css/. The same for Javascript folder.

{% stylesheets filter='less' output='css/core' 'public/css/*' %}

      <link href="{{ asset_url }}" type="text/css" rel="stylesheet" />

{% endstylesheets %}

{% javascripts "public/js/*" %}

      <script src="{{ asset_url }}" type="text/javascript"></script>

{% endjavascripts %}

Here all the files of /web/public folder are automatically loaded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜