Twig 根据条件扩展模板

发布于 2024-12-11 09:04:05 字数 637 浏览 0 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

多孤肩上扛 2024-12-18 09:04:05

试试这个:

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

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

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/

耀眼的星火 2024-12-18 09:04:05

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

{% extends parent_template_var %}

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

在控制器中定义 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

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

{% extends parent_template_var %}

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

Define parent_template_var in your controller:

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

牵你手 2024-12-18 09:04:05

来自官方文档的回答:

有条件继承

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

{% 扩展独立? "minimum.html" : "base.html" %}

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

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.

影子的影子 2024-12-18 09:04:05

您不能扩展多个模板,这就是您收到错误的原因,如果您想这样做,您需要将它们推送到如下所示的数组中。

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

峩卟喜欢 2024-12-18 09:04:05

这对于执行这个模板或那个模板都是有意义的。

但让我描述另一种情况。您有一个个人资料表单和一个用户可以上传个人资料相关文档的表单。由于个人资料表格已经很长,因此文档已转移到新表格。

一切都很好。现在我们要使用引导选项卡来执行配置文件|用户友好性的文档。

现在我知道了,因为我们使用两个单独的表格,如果您提交文档,配置文件上的更改将不会保存,反之亦然。

我已使用“viewOnly”在选项卡中添加了文档表单

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

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

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

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

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

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

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

当 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) }}

但现在在顶部,

{% extends viewOnly == 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

<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.

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

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

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:

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文