我在 Symfony 2.1.3 中使用 MopaBootstrapBundle 和 Twig 模板。 此捆绑包具有 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 /资源/引导/ JS /自举transition.js”。“) 在“MopaBootstrapBundle :: base.html.twig”。
我发现的是,如果你像这样扩展它:
{% 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 /资源/引导/ JS /自举typeahead.js”。“) 在“MopaBootstrapBundle :: base.html.twig”。
如果我只删除一行:
{% 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 /资源/引导/ JS /自举transition.js”。“) 在“MopaBootstrapBundle :: base.html.twig”。
它仍尝试从基本模板加载所有脚本。
有关如何覆盖* foot_script * block以使其为空并且不加载这些JS文件的任何建议吗?
答案 0 :(得分:1)
你想要的是嵌入 MopaBootstrapBundle::base.html.twig
而不是扩展它。您应该使用Twig的embed
标记:
{% embed 'MopaBootstrapBundle::base.html.twig' %}
{% block foot_script %}{% endblock foot_script %}
{% endembed %}
来自Twig的文档:
embed标签结合了include和extends的行为。它允许您包含另一个模板的内容,就像include一样。但它也允许您覆盖包含的模板中定义的任何块,例如扩展模板时。