php 有条件的树枝扩展模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7861438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:31:21  来源:igfitidea点击:

Twig extend template on condition

phptemplateslayoutsymfonytwig

提问by Dan Cearnau

I use Symfony 2 with Twig and my question is pretty straightforward:

我将 Symfony 2 与 Twig 一起使用,我的问题非常简单:

In a view I want to extend one of the layouts based on a variable. If the variable is falseI want to extend UdoWebsiteBundle::layout.html.twigand if it's trueI want to extend UdoWebsiteBundle::layout_true.html.twig.

在视图中,我想基于变量扩展其中一个布局。如果变量是false我想扩展UdoWebsiteBundle::layout.html.twig,如果是true我想扩展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

第 7 行的“UdoWebsiteBundle:home:home.html.twig”中禁止使用多个扩展标签

Is there any other way to achieve this?

有没有其他方法可以实现这一目标?

回答by Maerlyn

Try this one:

试试这个:

{% extends intro == 'false' 
    ? 'UdoWebsiteBundle::layout.html.twig' 
    : 'UdoWebsiteBundle::layout_true.html.twig' %}

Idea taken from here: http://jorisdewit.ca/2011/08/27/extending-different-layouts-for-ajax-requests-in-twig-symfony2/

取自这里的想法:http: //jorisdewit.ca/2011/08/27/extending-different-layouts-for-ajax-requests-in-twig-symfony2/

回答by svassr

To keep it neat you should use Twig dynamic inheritance support by using a variable, defined in your controller, as the base template:

为了保持整洁,您应该通过使用控制器中定义的变量作为基本模板来使用 Twig 动态继承支持:

{% extends parent_template_var %}

If the variable evaluates to a Twig_Template object, Twig will use it as the parent template.

如果变量计算为 Twig_Template 对象,Twig 将使用它作为父模板。

Define parent_template_varin your controller:

在您的控制器中定义parent_template_var

if($intro == 'false')
    $parent_template_var = 'UdoWebsiteBundle::layout.html.twig';
}else{
    $parent_template_var = 'UdoWebsiteBundle::layout_true.html.twig';
}
return $this->render('::/action.html.twig', array('parent_template_var' => $parent_template_var ));

http://twig.sensiolabs.org/doc/tags/extends.html

http://twig.sensiolabs.org/doc/tags/extends.html

回答by Benoit

Answer from the official documentation:

官方文档中的回答:

Conditional Inheritance

As the template name for the parent can be any valid Twig expression, it's possible to make the inheritance mechanism conditional:

{% extends standalone ? "minimum.html" : "base.html" %}

In this example, the template will extend the "minimum.html" layout template if the standalone variable evaluates to true, and "base.html" otherwise.

条件继承

由于父模板名称可以是任何有效的 Twig 表达式,因此可以使继承机制有条件:

{% extends standalone ? "minimum.html" : "base.html" %}

在此示例中,如果独立变量的计算结果为 true,则模板将扩展“minimum.html”布局模板,否则扩展“base.html”。

回答by dTHQb

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.

一切都很好。现在我们要使用引导选项卡来做 Profile | 用户友好性文档。

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

我已经使用

<div role="tabpanel" class="tab-pane" id="documents">
    {{ render(controller('ManyAppBundle:Document:createDocument', {'viewOnly': true})) }}
</div>

The 'viewOnly': true is a query parameter and is not required by the action.

'viewOnly': true 是查询参数,操作不需要。

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

我现在的问题是,如果配置文件选项卡呈现文档模板,它必须只显示上传小部件和提交,而当您直接转到文档页面时,它必须显示标题和侧栏以及所有内容。所以我确实尝试过

{% if not viewOnly %}
    {% extends ... %}
{% endif %}

That gave problems because you can't use extends within a if. Like you suggested in other answers try using

这带来了问题,因为您不能在 if 中使用扩展。就像您在其他答案中建议的那样尝试使用

{% extends viewOnly == true ? ... %}

This reolved the Twig issue up to the execution of the code when viewOnly is false.

这解决了当 viewOnly 为 false 时执行代码的 Twig 问题。

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:

当 viewOnly 为 false 时,它​​必须扩展所有其他模板使用的基本模板,但如果为 true,我只想显示:

{{ form_start(form, { 'style': 'horizontal', 'col_size': 'sm' }) }}
    {% if form.documents is defined %}
        {{ form_row(form.documents) }}
    {% endif %}

    {{ form_row(form.submit, { 'attr': { 'class': 'btn btn-success' } }) }}
{{ form_end(form) }}

But now with the top

但现在与顶部

{% extends viewOnly == true ? ... %}

if viewOnly becomes false it fails with Template "" can't be find.

如果 viewOnly 变为 false,则它会因找不到模板“”而失败。

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?

或者,当 viewOnly 为 true 但失败时没有任何反应时,是否有一种方法可以扩展它?

回答by bazingabazinga

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'] %}

{% 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

但是您需要使用 Twig 1.2 版来执行此操作。 树枝文档