如何将代码从包含的模板附加到父模板 [英] How to append code to parent template from included template

查看:32
本文介绍了如何将代码从包含的模板附加到父模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

dashboard.html.twig:

dashboard.html.twig:

<% extends layout.twig.html %>
<% include ('filter.html.twig') %>
<% block javascripts %>
< %endblock >

filter.html.twig:

filter.html.twig:

filter form definition and html
filter form definition and html
filter form definition and html

<% block javascripts %>
javascript for use in filter form
javascript for use in filter form
<% endblock %>

我希望在布局中设置过滤器的 javascript,因为它是在 JQuery 之后加载的.但是 Javascript 是在过滤器表单之后立即呈现的,因此获取 $ 是未定义的.

I want the filter's javascript to be set in the layout, because that way it is loaded AFTER the JQuery. But the Javascript is rendered right after the filter form, so getting $ is undefined.

所以我想要的是 filter.html.twig 中定义的 Javascript 来覆盖 layout.html.twig 中的块,与 extends 相同>.

So whatI want is the Javascript defined in filter.html.twig to override the block in layout.html.twig, same as extends.

谢谢!

推荐答案

包含的模板不能改变它们包含的块.

我发现我使用延迟块扩展的最佳"方式.它延迟了块的渲染.如果您按照我的链接中的 advanced 示例进行操作看你怎么解决

Included templates can't alter the blocks of their includer.

The "best" way I found i using the deferred block extension. It delays the render of a block. If you follow the advanced example in my link you can see how you could solve it

这篇关于如何将代码从包含的模板附加到父模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆