5

我在Symfony 2.1.3中使用MopaBootstrapBundleTwig模板。这个包有base.html.twig模板,其中包含脚本块:

{% block foot_script %}
    {# To only use a subset or add more js overwrite and copy paste this block
    To speed up page loads save a copy of jQuery in your project and override this block to include the correct path
    Otherwise the regeneration is done on every load in dev more with use_controller: true
     #}
    {% javascripts
        'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-transition.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-modal.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-dropdown.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-scrollspy.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-tab.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-tooltip.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-popover.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-alert.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-button.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-collapse.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-carousel.js'
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-typeahead.js'
        '@MopaBootstrapBundle/Resources/public/js/mopabootstrap-collection.js'
        '@MopaBootstrapBundle/Resources/public/js/mopabootstrap-subnav.js'
    %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock foot_script %}

我正在使用以下方法在我的模板中扩展它:

{% extends 'MopaBootstrapBundle::base.html.twig' %}
{% block foot_script %}{% endblock foot_script %}

但它仍然尝试加载 Bundle 的base.html.twig模板,我得到:

在“MopaBootstrapBundle::base.html.twig”中编译模板(“无法找到文件“@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-transition.js”。”)期间引发了异常。


我发现,如果你像这样扩展它:

{% extends 'MopaBootstrapBundle::base.html.twig' %}
{% block foot_script %}
    {% javascripts
        '@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-typeahead.js'
        '@MopaBootstrapBundle/Resources/public/js/mopabootstrap-collection.js'
        '@MopaBootstrapBundle/Resources/public/js/mopabootstrap-subnav.js'
    %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock foot_script %}

注意typeahead.js

我得到:

编译“MopaBootstrapBundle::base.html.twig”中的模板(“无法找到文件“@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-typeahead.js”。”)时引发异常。

如果我只删除一行:

{% extends 'MopaBootstrapBundle::base.html.twig' %}
{% block foot_script %}
    {% javascripts
        '@MopaBootstrapBundle/Resources/public/js/mopabootstrap-collection.js'
        '@MopaBootstrapBundle/Resources/public/js/mopabootstrap-subnav.js'
    %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
{% endblock foot_script %}

我得到:

在“MopaBootstrapBundle::base.html.twig”中编译模板(“无法找到文件“@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-transition.js”。”)期间引发了异常。

它仍然尝试从基本模板加载所有脚本。

任何建议如何覆盖 *foot_script* 块以使其为空而不加载这些 JS 文件?

4

1 回答 1

1

你想要的是嵌入MopaBootstrapBundle::base.html.twig不是扩展它。你应该使用 Twig 的embed标签:

{% embed 'MopaBootstrapBundle::base.html.twig' %}
    {% block foot_script %}{% endblock foot_script %}
{% endembed %}

来自 Twig 的文档:

embed 标签结合了 include 和 extends 的行为。它允许您包含另一个模板的内容,就像 include 一样。但它也允许您覆盖在包含的模板中定义的任何块,例如在扩展模板时。

于 2012-11-21T18:19:46.237 回答