8000 [TwigBundle] added a compatibility layer for the render tag so that t… · symfony/symfony@a5e1c4a · GitHub
[go: up one dir, main page]

Skip to content

Commit a5e1c4a

Browse files
committed
[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.
1 parent 84ec187 commit a5e1c4a

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2 10000 +
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Tests\Node;
13+
14+
use Symfony\Bundle\TwigBundle\Tests\TestCase;
15+
use Symfony\Bundle\TwigBundle\TokenParser\RenderTokenParser;
16+
use Symfony\Bundle\TwigBundle\Node\RenderNode;
17+
18+
class RenderTokenParserTest extends TestCase
19+
{
20+
/**
21+
* @dataProvider getTestsForRender
22+
*/
23+
public function testCompile($source, $expected)
24+
{
25+
$env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
26+
$env->addTokenParser(new RenderTokenParser());
27+
$stream = $env->tokenize($source);
28+
$parser = new \Twig_Parser($env);
29+
30+
$this->assertEquals($expected, $parser->parse($stream)->getNode('body')->getNode(0));
31+
}
32+
33+
public function getTestsForRender()
34+
{
35+
return array(
36+
array(
37+
'{% render "foo" %}',
38+
new RenderNode(
39+
new \Twig_Node_Expression_Constant('foo', 1),
40+
new \Twig_Node_Expression_Array(array(), 1),
41+
1,
42+
'render'
43+
)
44+
),
45+
array(
46+
'{% render "foo", {foo: 1} %}',
47+
new RenderNode(
48+
new \Twig_Node_Expression_Constant('foo', 1),
49+
new \Twig_Node_Expression_Array(array(
50+
new \Twig_Node_Expression_Constant('foo', 1),
51+
new \Twig_Node_Expression_Constant('1', 1),
52+
), 1),
53+
1,
54+
'render'
55+
)
56+
),
57+
// deprecated in 2.2
58+
array(
59+
'{% render "foo" with {foo: 2} %}',
60+
new RenderNode(
61+
new \Twig_Node_Expression_Constant('foo', 1),
62+
new \Twig_Node_Expression_Array(array(), 1),
63+
1,
64+
'render'
65+
)
66+
),
67+
// deprecated in 2.2
68+
array(
69+
'{% render "foo" with {foo: 2}, {foo: 1} %}',
70+
new RenderNode(
71+
new \Twig_Node_Expression_Constant('foo', 1),
72+
new \Twig_Node_Expression_Array(array(
73+
new \Twig_Node_Expression_Constant('foo', 1),
74+
new \Twig_Node_Expression_Constant('1', 1),
75+
), 1),
76+
1,
77+
'render'
78+
)
79+
),
80+
);
81+
}
82+
}

src/Symfony/Bundle/TwigBundle/TokenParser/RenderTokenParser.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ public function parse(\Twig_Token $token)
3131
{
3232
$expr = $this->parser->getExpressionParser()->parseExpression();
3333

34+
// attributes (not used anymore, kept for BC reasons)
35+
// @deprecated in 2.2 and will be removed in 2.3
36+
if ($this->parser->getStream()->test(\Twig_Token::NAME_TYPE, 'with')) {
37+
$this->parser->getStream()->next();
38+
$attributes = $this->parser->getExpressionParser()->parseExpression();
39+
} else {
40+
$attributes = new \Twig_Node_Expression_Array(array(), $token->getLine());
41+
}
42+
3443
// options
3544
if ($this->parser->getStream()->test(\Twig_Token::PUNCTUATION_TYPE, ',')) {
3645
$this->parser->getStream()->next();

0 commit comments

Comments
 (0)
0