为什么Twig extend标签有时不会;我看不到一些变量


Why Twig extend tag sometimes doesn't see some variables?

在Symfony2中,我创建了一个简单的操作:

public function testAction()
{        
    return $this->render('TestBundle::test.html.twig', array(
        'testBool'    => true,
        'testTplPath' => 'TestBundle::base.html.twig'
    ));
}

base.html.twig:

<html><body>{% block content %}{% endblock %}</body></html>

现在我的test.html.twig模板中的extends标签有问题,如果我在这个标签中使用简单的字符串,比如:{% extends 'TestBundle::base.html.twig' %},一切都很好,但这不起作用:{% extends testTplPath %}。第二个例子抛出错误:Variable "testTplPath" does not exist in TestBundle::test.html.twig at line 1

这很好用:{% extends testBool ? 'TestBundle::base.html.twig' : 'blah' %}

这抛出了异常:{% extends testBool ? testTplPath : 'blah' %}-Variable "testBool" does not exist in TestBundle::test.html.twig at line 1(注意,Twig这次抛出了关于testBool变量的异常-为什么?!)

根据TWIG的文件,上面的所有例子都应该有效,我不知道我做错了什么。

我做了很多测试,当exted看到变量时和不看到变量时,我都想不出任何模式。

我目前使用Twig v1.16.0和Symfony 2.5.4


更新:我注意到另一件奇怪的事情,如果我将有效路径放入testTplPath或创建任何其他返回有效路径的动态表达式,我会得到Variable "[first var in expression]" does not exist异常,但如果它返回无效路径,我会获得:Unable to find template "TestBundle::SomeInvalidTemplate.html.twig"

我发现了问题。

在templatetest.html.twig中,我也有全局表单,我确信这个表单并不重要,但。。。https://github.com/symfony/symfony/issues/5284看起来这个bug还没有完全修复。我用[_self,form_theme]%}从{%form_theme表单中删除了_self,现在一切如常。