From 3c5c51260eed80ef4f0ac0ba5d54b81817fb0ed6 Mon Sep 17 00:00:00 2001 From: Martin Hason Date: Wed, 27 Jul 2011 08:19:59 +0200 Subject: [PATCH] fixed optimization for rendering blocks with variable name --- lib/Twig/NodeVisitor/Optimizer.php | 7 ++++++- test/Twig/Tests/NodeVisitor/OptimizerTest.php | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletions(-) diff --git a/lib/Twig/NodeVisitor/Optimizer.php b/lib/Twig/NodeVisitor/Optimizer.php index 7348734..8a9a35a 100644 --- a/lib/Twig/NodeVisitor/Optimizer.php +++ b/lib/Twig/NodeVisitor/Optimizer.php @@ -83,7 +83,12 @@ class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface protected function optimizeRenderBlock($node, $env) { if ($node instanceof Twig_Node_Print && $node->getNode('expr') instanceof Twig_Node_Expression_BlockReference) { - return new Twig_Node_BlockReference($node->getNode('expr')->getNode('name')->getAttribute('value'), $node->getLine(), $node->getNodeTag()); + $name = $node->getNode('expr')->getNode('name'); + if ($name instanceof Twig_Node_Expression_Constant) { + return new Twig_Node_BlockReference($name->getAttribute('value'), $node->getLine(), $node->getNodeTag()); + } else { + return new Twig_Node_BlockReference($name, $node->getLine(), $node->getNodeTag()); + } } return $node; diff --git a/test/Twig/Tests/NodeVisitor/OptimizerTest.php b/test/Twig/Tests/NodeVisitor/OptimizerTest.php index 9309165..17e171a 100644 --- a/test/Twig/Tests/NodeVisitor/OptimizerTest.php +++ b/test/Twig/Tests/NodeVisitor/OptimizerTest.php @@ -20,6 +20,15 @@ class Twig_Tests_NodeVisitor_OptimizerTest extends PHPUnit_Framework_TestCase $this->assertInstanceOf('Twig_Node_BlockReference', $stream->getNode('body')); } + public function testRenderVariableBlockOptimizer() + { + $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env->addExtension(new Twig_Extension_Optimizer()); + $stream = $env->parse($env->tokenize('{{ block(name|lower) }}', 'index')); + + $this->assertInstanceOf('Twig_Node_BlockReference', $stream->getNode('body')); + } + /** * @dataProvider getTestsForForOptimizer */ -- 1.7.2.5