dongshan6870 2014-03-28 18:27
浏览 44
已采纳

在Symfony2上包含多个资产的最佳方法

I' m usign the same amount of assets in my symfony2 project. If I upgrade a plugin version, all files must be modified. So, which is the best way to add assets on twig templates using Symfony2 framework?

{% block javascriptCodigo %}
    {{ parent() }}
    <script type="text/javascript" src="{{ asset('bundles/Bundle/js/ajaxLoadTables/ajaxLoadTables.js') }}"></script>
    <script type="text/javascript" src="{{ asset('bundles/Bundle/js/jquery-loadmask/jquery.loadmask.min.js') }}"></script>
    <script type="text/javascript" src="{{ asset('bundles/Bundle/js/jquery-validate/jquery.validate.min.js') }}"></script>
    <script type="text/javascript" src="{{ asset('bundles/Bundle/js/jquery-validate/localization/messages_es.js') }}"></script>
    <script type="text/javascript" src="{{ asset('bundles/Bundle/js/bootstrap3-dialog/js/bootstrap-dialog.min.js') }}"></script>
...
  • 写回答

1条回答 默认 最新

  • dony113407 2014-03-28 18:45
    关注

    The best way is to use AsseticBundle. All is described in details in doc.

    In general, while assetic is enabled you can do something like

    {% block javascripts %}
        {{ parent() }}
        {% javascripts
            '@AcmeDemoBundle/Resources/public/js/knockout.jquery-bindings.js'
            '@AcmeDemoBundle/Resources/public/js/jquery.validate.min.js'
            '@AcmeDemoBundle/Resources/public/js/jquery.validate.password.js'
        %}
        <script src="{{ asset_url }}"></script>
        {% endjavascripts %} 
    {% endblock %}
    

    This will combine all your assets to one file. You can also use this for stylesheets by using stylesheets function. There is also easy way to add version number for your query url (for asset) which will solve browser cache issues.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?