简体   繁体   English

为什么Twig扩展标签有时看不到一些变量?

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

In Symfony2 i created simple action: 在Symfony2中,我创建了简单的操作:

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

base.html.twig: base.html.twig:

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

And now i have problem with extends tag in my test.html.twig template, if i use simple string in this tag like this: {% extends 'TestBundle::base.html.twig' %} everything works fine but this dosn't work: {% extends testTplPath %} . 现在我的test.html.twig模板中的扩展标签有问题,如果我在这个标签中使用简单的字符串,如下所示: {% extends 'TestBundle::base.html.twig' %}一切正常,但这个dosn'工作: {% extends testTplPath %} Second example throws me error: Variable "testTplPath" does not exist in TestBundle::test.html.twig at line 1 . 第二个例子引发了我的错误: Variable "testTplPath" does not exist in TestBundle::test.html.twig at line 1

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

This throws exception: {% extends testBool ? testTplPath : 'blah' %} 抛出异常: {% extends testBool ? testTplPath : 'blah' %} {% extends testBool ? testTplPath : 'blah' %} - Variable "testBool" does not exist in TestBundle::test.html.twig at line 1 (notice that Twig throws this time exception about testBool variable - why?!) {% extends testBool ? testTplPath : 'blah' %} - Variable "testBool" does not exist in TestBundle::test.html.twig at line 1 (请注意,Twig会抛出有关testBool变量的时间异常 - 为什么?!)

According to TWIG documentation all examples abowe should work and i don't know what i did wrong. 根据TWIG文件,abowe的所有例子都应该有效,我不知道我做错了什么。

I made a lot of tests and i can't figure any pattern when exteds see variables and when not. 我做了很多测试,当exted看到变量时,我无法想出任何模式。

I currently use Twig v1.16.0 and Symfony 2.5.4 我目前使用Twig v1.16.0和Symfony 2.5.4


Update : I noticed antoher strange thing, if I put into testTplPath valid path or create any other dynamic experssion which returns valid path i got Variable "[first var in expression]" does not exist exception but if it return invalid path i got: Unable to find template "TestBundle::SomeInvalidTemplate.html.twig" . 更新 :我注意到antoher奇怪的事情,如果我放入testTplPath有效路径或创建任何其他动态testTplPath返回有效路径我得到Variable "[first var in expression]" does not exist异常,但如果它返回无效路径我得到: Unable to find template "TestBundle::SomeInvalidTemplate.html.twig"

I found the problem. 我发现了这个问题。

In template test.html.twig i had also global form and I was convinced that this form is not important but... https://github.com/symfony/symfony/issues/5284 It looks that this bug is still not fixed completely. 在模板test.html.twig我也有全局形式,我确信这个形式并不重要,但是...... https://github.com/symfony/symfony/issues/5284看来这个bug还没有解决完全。 I removed _self from {% form_theme form with [_self, form_theme] %} and everything works as expected now. 我使用[_self,form_theme]%}从{%form_theme表单中删除了_self,现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM