细枝根据条件扩展模板 [英] Twig extend template on condition

查看:76
本文介绍了细枝根据条件扩展模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

在视图中,我想基于变量扩展布局之一.如果变量是false,我想扩展UdoWebsiteBundle::layout.html.twig,如果它是true,我想扩展UdoWebsiteBundle::layout_true.html.twig.

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.

这是我尝试的代码:

{% 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"中禁止使用多个扩展标签

Multiple extends tags are forbidden in "UdoWebsiteBundle:home:home.html.twig" at line 7

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

Is there any other way to achieve this?

推荐答案

尝试以下方法:

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

从此处获取的想法: 查看全文

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