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

在一个 View 中,我想基于变量扩展布局之一。如果变量是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 %}

我收到此错误:



还有其他方法可以做到这一点吗?

最佳答案

试试这个:

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

08-27 16:53
查看更多