gpt4 book ai didi

symfony - 无法将扩展 Twig 模板 block 覆盖为空

转载 作者:行者123 更新时间:2023-12-04 21:46:49 25 4
gpt4 key购买 nike

我在 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 模板,我得到:

An exception has been thrown during the compilation of a template ("Unable to find file "@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-transition.js".") in "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

我得到:

An exception has been thrown during the compilation of a template ("Unable to find file "@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-typeahead.js".") in "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 %}

我得到:

An exception has been thrown during the compilation of a template ("Unable to find file "@MopaBootstrapBundle/Resources/bootstrap/js/bootstrap-transition.js".") in "MopaBootstrapBundle::base.html.twig".



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

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

最佳答案

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

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

来自 Twig 的文档:

The embed tag combines the behaviour of include and extends. It allows you to include another template's contents, just like include does. But it also allows you to override any block defined inside the included template, like when extending a template.

关于symfony - 无法将扩展 Twig 模板 block 覆盖为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13233476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com