将来自多个模板的 javascripts 与 Assetic 结合起来

Combine javascripts from multiple templates with Assetic

假设我有以下树枝模板:

base.html.twig:

<html>
    <body>
    {% block javascripts %}
        {% javascripts
            '../app/Resources/js/jquery-2.2.4.min.js'
            '../app/Resources/js/base.js'
            filter='uglifyjs2' output='main.js'
        %}
            <script src="{{ asset_url }}" type="text/javascript"></script>
        {% endjavascripts %}
    {% endblock javascripts %}
    </body>
</html>

page1.html.twig:

{% extends 'base.html.twig' %}

{% block javascripts %}
    {{ parent() }}
    {% javascripts
        '../app/Resources/js/page1_specific.js'
        '../app/Resources/js/page1_other.js'
        filter='uglifyjs2' output='page1.js'
    %}
        <script src="{{ asset_url }}" type="text/javascript"></script>
    {% endjavascripts %}
{% endblock javascripts %}

使用此代码,page1 模板将发出两个 javascript 请求,一个用于 main.js,另一个用于 page1.js

是否可以不同地编写它,所以我将只生成一个特定于每个最终模板的大 js 文件,包括基本页面 js 和 page1 特定的 js?

您可以在 config.yml 基础 js 中声明,如下所示:

assetic:
    assets:
        base:
        inputs:
            - '@AppBundle/Resources/public/js/jquery-2.2.4.min.js'
            - '@AppBundle/Resources/public/js/base.js'

并在您的 javascript 块中调用它:

{% javascripts
    '@base'
    '../app/Resources/js/page1_specific.js'
    '../app/Resources/js/page1_other.js'
    filter='uglifyjs2' output='page1.js'
%}

您可以找到更多 info here