Twig 根据条件扩展模板
我将 Symfony 2 与 Twig 一起使用,我的问题非常简单:
在视图中,我想基于变量扩展其中一个布局。如果变量为 false
我想扩展 UdoWebsiteBundle::layout.html.twig
如果它是 true
我想扩展 UdoWebsiteBundle ::layout_true.html.twig
。
这是我尝试过的代码:
{% block layout_extender %}
{% if intro == 'false' %}
{% extends 'UdoWebsiteBundle::layout.html.twig' %}
{% else %}
{% extends 'UdoWebsiteBundle::layout_true.html.twig' %}
{% endif %}
{% endblock %}
我收到此错误:
第 7 行的“UdoWebsiteBundle:home:home.html.twig”中禁止使用多个扩展标签
还有其他方法可以实现此目的吗?
I use Symfony 2 with Twig and my question is pretty straightforward:
In a view I want to extend one of the layouts based on a variable. If the variable is false
I want to extend UdoWebsiteBundle::layout.html.twig
and if it's true
I want to extend UdoWebsiteBundle::layout_true.html.twig
.
Here is the code I tried:
{% block layout_extender %}
{% if intro == 'false' %}
{% extends 'UdoWebsiteBundle::layout.html.twig' %}
{% else %}
{% extends 'UdoWebsiteBundle::layout_true.html.twig' %}
{% endif %}
{% endblock %}
I get this error:
Multiple extends tags are forbidden in "UdoWebsiteBundle:home:home.html.twig" at line 7
Is there any other way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个:
想法取自这里:http://jorisdewit.ca/2011/08/27/extending- Different-layouts-for-ajax-requests-in-twig-symfony2/
Try this one:
Idea taken from here: http://jorisdewit.ca/2011/08/27/extending-different-layouts-for-ajax-requests-in-twig-symfony2/
为了保持整洁,您应该通过使用控制器中定义的变量作为基本模板来使用 Twig 动态继承支持:
如果变量计算结果为 Twig_Template 对象,Twig 将使用它作为父模板。
在控制器中定义 parent_template_var:
http://twig.sensiolabs。 org/doc/tags/extends.html
To keep it neat you should use Twig dynamic inheritance support by using a variable, defined in your controller, as the base template:
If the variable evaluates to a Twig_Template object, Twig will use it as the parent template.
Define parent_template_var in your controller:
http://twig.sensiolabs.org/doc/tags/extends.html
来自官方文档的回答:
Answer from the official documentation:
您不能扩展多个模板,这就是您收到错误的原因,如果您想这样做,您需要将它们推送到如下所示的数组中。
{% extends ['MyAppCustomBundle::Layout/layout.html.twig', 'FOSUserBundle::layout.html.twig'] %}
但您需要使用 Twig 版本 1.2 来执行此操作。
twig 文档
You cannot extends multiple template, that's why you've got the error, if you want to so, you need to push them in an array like below.
{% extends ['MyAppCustomBundle::Layout/layout.html.twig', 'FOSUserBundle::layout.html.twig'] %}
But you will need to use Twig version 1.2 to do it.
twig documentation
这对于执行这个模板或那个模板都是有意义的。
但让我描述另一种情况。您有一个个人资料表单和一个用户可以上传个人资料相关文档的表单。由于个人资料表格已经很长,因此文档已转移到新表格。
一切都很好。现在我们要使用引导选项卡来执行配置文件|用户友好性的文档。
现在我知道了,因为我们使用两个单独的表格,如果您提交文档,配置文件上的更改将不会保存,反之亦然。
我已使用“viewOnly”在选项卡中添加了文档表单
:true 是查询参数,操作不需要。
我现在的问题是,如果配置文件选项卡呈现文档模板,它必须只显示上传小部件和提交,而当您直接进入文档页面时,它必须显示标题和侧栏以及所有内容。所以我确实尝试过
这会带来问题,因为你不能在 if 中使用扩展。就像您在其他答案中建议的那样,尝试使用
This 解决了 viewOnly 为 false 时执行代码的 Twig 问题。
当 viewOnly 为 false 时,它必须扩展所有其他模板使用的基本模板,但如果为 true,我只想显示这一点:
但现在在顶部,
如果 viewOnly 变为 false,则会失败,并显示 Template "" can't be find。
有没有办法说扩展此特定模板与不扩展任何模板的结果相同?
或者有没有一种方法可以说当 viewOnly true 时扩展此但失败时没有任何反应?
This all makes sense to do either this template or that template.
But let me describe another situation. You have a profile form and a form where users can upload personal profile related documents. Since the profile form is already very long the documents moved to a new form.
Everything works great. Now we want to use the bootstrap tabs to do Profile | Documents for user friendliness.
Now I know because we are using two seperate forms if you submit the documents the changes on the profile won't save and vice versa.
I have added the document form in the tab using
The 'viewOnly': true is a query parameter and is not required by the action.
My question now becomes if the profile tab renders the document template it must only show the upload widget and the submit where as when you go directly to the document page it must show the title and side bar and everything. So I did try
That gave problems because you can't use extends within a if. Like you suggested in other answers try using
This reolved the Twig issue up to the execution of the code when viewOnly is false.
When viewOnly is false it must extend the base template used by all other templates but if it is true I only want to show this:
But now with the top
if viewOnly becomes false it fails with Template "" can't be find.
Is there a way to say extends this specific template that will be the same result of not extending any template?
Or alternatively is there a way of saying extend this when viewOnly true but nothing happens on the fail?