fixed optimization for rendering blocks with variable name
authorMartin Hason <martin.hason@gmail.com>
Wed, 27 Jul 2011 06:19:59 +0000 (08:19 +0200)
committerMartin Hason <martin.hason@gmail.com>
Wed, 27 Jul 2011 06:19:59 +0000 (08:19 +0200)
lib/Twig/NodeVisitor/Optimizer.php
test/Twig/Tests/NodeVisitor/OptimizerTest.php

index 7348734..8a9a35a 100644 (file)
@@ -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;
index 9309165..17e171a 100644 (file)
@@ -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
      */