我的问题:
我有 3 个模板:
-
main.html.twig
(主布局文件)
-
layout.html.twig
(捆绑包特定的布局覆盖,其中包含一些捆绑包特定的 JS 标签)
-
create.html.twig
(页面特定的模板文件,还包含一些页面特定的 JS 标签)
我在我的基本布局中定义一个名为“javascript”的块(main.html.twig
),然后覆盖它(但是在 layout.html.twig
中调用 {{parent() }}
。这工作正常,并且主模板文件中的 JS 标记仍然包含在 然后,我在 create.html.twig
文件
中执行相同的操作,按如下方式重写该块:
{% block javascripts %}
{{ parent() }}
{% javascripts '@BundleName/Resources/public/js/application.album.uploader.js'
'@BundleName/Resources/public/js/jquery.uploadify.js'
'@BundleName/Resources/public/js/swfuploadify.js' filter='?yui_js' %}
<script src='{{ asset_url }}' type='text/javascript'></script>
{% endjavascripts %}
{% endblock %}
此时,而不是仅仅重写父级中的 javascript 块(layout.html.twig
) 并包括其上方模板中定义的所有脚本,它执行以下操作:
- 转储
> 标签位于输出中间(这会导致错误,因为在我的 main.html.twig
文件中,我只在 HTML 标记的末尾包含 jQuery 库
- 然后它还会转储脚本和其他人一起出去(因为我 我不知道
是什么导致脚本被转储到 create.html.twig 模板的中间,而且我也很困惑为什么它们被转储到屏幕两次(一次在创建的中间,然后一次在底部以及来自 main.html.twig
和 layout.html.twig
的所有其余脚本代码>.
有人有什么想法吗?如果有任何不清楚的地方或者我是否可以提供更多信息,请告诉我。
编辑:
文件内容如下...
main.html.twig: https://gist.github.com/7f29353eaca0947528ce< /a>
布局.html.twig: https://gist.github.com/734947e9118b7765715e
create.html.twig: https://gist.github.com/c60c8d5c61e00ff86912
编辑2:
今天早上我再次查看了这个问题,看起来它对样式表做了同样的事情。我尝试在 layout.html.twig
中定义一个名为 pagescripts
的新块,然后在我的 create.html.twig
中使用该块,但是这有相同的结果,它似乎只是在我使用的地方转储脚本和样式表
{% block pagescripts %}
(scripts here)
{% endblock}
My problem:
I have 3 templates:
main.html.twig
(main layout file)
layout.html.twig
(a bundle specific layout override which contains some bundle specific JS tags)
create.html.twig
(a page specific template file which also contains some page specific JS tags)
I am defining a block called 'javascript' in my base layout (main.html.twig
), then overriding it (but calling {{ parent() }}
in layout.html.twig
. This works fine, and the JS tags from the main template file are still included above those in the layout.html.twig
template.
I then do the same in the create.html.twig
file, overriding the block as follows:
{% block javascripts %}
{{ parent() }}
{% javascripts '@BundleName/Resources/public/js/application.album.uploader.js'
'@BundleName/Resources/public/js/jquery.uploadify.js'
'@BundleName/Resources/public/js/swfuploadify.js' filter='?yui_js' %}
<script src='{{ asset_url }}' type='text/javascript'></script>
{% endjavascripts %}
{% endblock %}
At this point, instead of just overriding the javascript block in the parent (layout.html.twig
) and including all the scripts defined in the templates above it, it does the following:
- Dumps the
<script>
tags in the middle of the output (which causes an error, because in my main.html.twig
file I am only including the jQuery library at the end of the HTML markup
- Then it also dumps the scripts out along with the rest of the others (as I would expect it to)
I am not sure what is causing the scripts to be dumped in the middle of the create.html.twig
template, and I'm also confused as to why they're being dumped to the screen twice (once in the middle of the create and then once at the bottom along with all the rest of my scripts from main.html.twig
and layout.html.twig
.
Has anyone got any ideas? Let me know if anything is unclear or if I can provide some more information.
EDIT:
File contents are below...
main.html.twig: https://gist.github.com/7f29353eaca0947528ce
layout.html.twig: https://gist.github.com/734947e9118b7765715e
create.html.twig: https://gist.github.com/c60c8d5c61e00ff86912
EDIT 2:
I've been having another look at the issue this morning and it looks as though its doing the same thing for stylesheets. I tried to define a new block called pagescripts
in my layout.html.twig
and then use the block in my create.html.twig
but this had the same outcome, it just seems to dump the scripts and stylesheets wherever I use the
{% block pagescripts %}
(scripts here)
{% endblock}
发布评论
评论(2)
我发现了这个问题。在
create.html.twig
中,我在{% block content %}
内部定义了{% block javascripts %}
内容,所以我假设 Twig 正在渲染content
块内的javascripts
块的输出。将
{% block javascripts %}
内容移到{% block content %}
块之外解决了该问题。I found the issue. In
create.html.twig
I was defining my{% block javascripts %}
content inside inside my{% block content %}
, so I assume Twig was rendering the output of thejavascripts
block inside thecontent
block.Moving the
{% block javascripts %}
content outside of the{% block content %}
block fixed the issue.这是 main.html.twig 的示例:
进入您的 create.html.twig
如果仍然有问题,您可以添加文档准备功能:
Here is an example of main.html.twig:
into your create.html.twig
If you still have issue, you can add a document ready function :