[TwigBundle] added a compatibility layer for the render tag so that the same code can work in both 2.1 and 2.2

If you want your code to work on both version, use the following syntax:

{% render url('foo') with {}, {'bar': 1} %}

where the empty array is not used in 2.2.
This commit is contained in:
Fabien Potencier 2012-12-21 08:47:43 +01:00
parent 84ec18783e
commit a5e1c4a657
2 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,82 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\Twig\Tests\Node;
use Symfony\Bundle\TwigBundle\Tests\TestCase;
use Symfony\Bundle\TwigBundle\TokenParser\RenderTokenParser;
use Symfony\Bundle\TwigBundle\Node\RenderNode;
class RenderTokenParserTest extends TestCase
{
/**
* @dataProvider getTestsForRender
*/
public function testCompile($source, $expected)
{
$env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
$env->addTokenParser(new RenderTokenParser());
$stream = $env->tokenize($source);
$parser = new \Twig_Parser($env);
$this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0));
}
public function getTestsForRender()
{
return array(
array(
'{% render "foo" %}',
new RenderNode(
new \Twig_Node_Expression_Constant('foo', 1),
new \Twig_Node_Expression_Array(array(), 1),
1,
'render'
)
),
array(
'{% render "foo", {foo: 1} %}',
new RenderNode(
new \Twig_Node_Expression_Constant('foo', 1),
new \Twig_Node_Expression_Array(array(
new \Twig_Node_Expression_Constant('foo', 1),
new \Twig_Node_Expression_Constant('1', 1),
), 1),
1,
'render'
)
),
// deprecated in 2.2
array(
'{% render "foo" with {foo: 2} %}',
new RenderNode(
new \Twig_Node_Expression_Constant('foo', 1),
new \Twig_Node_Expression_Array(array(), 1),
1,
'render'
)
),
// deprecated in 2.2
array(
'{% render "foo" with {foo: 2}, {foo: 1} %}',
new RenderNode(
new \Twig_Node_Expression_Constant('foo', 1),
new \Twig_Node_Expression_Array(array(
new \Twig_Node_Expression_Constant('foo', 1),
new \Twig_Node_Expression_Constant('1', 1),
), 1),
1,
'render'
)
),
);
}
}

View File

@ -31,6 +31,15 @@ class RenderTokenParser extends \Twig_TokenParser
{
$expr = $this->parser->getExpressionParser()->parseExpression();
// attributes (not used anymore, kept for BC reasons)
// @deprecated in 2.2 and will be removed in 2.3
if ($this->parser->getStream()->test(\Twig_Token::NAME_TYPE, 'with')) {
$this->parser->getStream()->next();
$attributes = $this->parser->getExpressionParser()->parseExpression();
} else {
$attributes = new \Twig_Node_Expression_Array(array(), $token->getLine());
}
// options
if ($this->parser->getStream()->test(\Twig_Token::PUNCTUATION_TYPE, ',')) {
$this->parser->getStream()->next();