Browse code

fixed block() is defined call when using a template argument

Fabien Potencier authored on 18/11/2016 20:44:11
Showing 1 changed files
... ...
@@ -39,47 +39,46 @@ class Twig_Node_Expression_BlockReference extends Twig_Node_Expression
39 39
     public function compile(Twig_Compiler $compiler)
40 40
     {
41 41
         if ($this->getAttribute('is_defined_test')) {
42
-            $compiler
43
-                ->raw('$this->hasBlock(')
44
-                ->subcompile($this->getNode('name'))
45
-                ->raw(', $context, $blocks)')
42
+            $this
43
+                ->compileTemplateCall($compiler, 'hasBlock')
44
+                ->compileBlockArguments($compiler)
46 45
             ;
47 46
         } else {
48 47
             if ($this->getAttribute('output')) {
49 48
                 $compiler->addDebugInfo($this);
50 49
 
51 50
                 $this
52
-                    ->compileTemplateCall($compiler)
53
-                    ->raw('->displayBlock')
54
-                ;
55
-                $this
51
+                    ->compileTemplateCall($compiler, 'displayBlock')
56 52
                     ->compileBlockArguments($compiler)
57 53
                     ->raw(";\n");
58 54
             } else {
59 55
                 $this
60
-                    ->compileTemplateCall($compiler)
61
-                    ->raw('->renderBlock')
56
+                    ->compileTemplateCall($compiler, 'renderBlock')
57
+                    ->compileBlockArguments($compiler)
62 58
                 ;
63
-                $this->compileBlockArguments($compiler);
64 59
             }
65 60
         }
66 61
     }
67 62
 
68
-    private function compileTemplateCall(Twig_Compiler $compiler)
63
+    private function compileTemplateCall(Twig_Compiler $compiler, $method)
69 64
     {
70 65
         if (!$this->hasNode('template')) {
71
-            return $compiler->write('$this');
66
+            $compiler->write('$this');
67
+        } else {
68
+            $compiler
69
+                ->write('$this->loadTemplate(')
70
+                ->subcompile($this->getNode('template'))
71
+                ->raw(', ')
72
+                ->repr($this->getTemplateName())
73
+                ->raw(', ')
74
+                ->repr($this->getTemplateLine())
75
+                ->raw(')')
76
+            ;
72 77
         }
73 78
 
74
-        return $compiler
75
-            ->write('$this->loadTemplate(')
76
-            ->subcompile($this->getNode('template'))
77
-            ->raw(', ')
78
-            ->repr($this->getTemplateName())
79
-            ->raw(', ')
80
-            ->repr($this->getTemplateLine())
81
-            ->raw(')')
82
-        ;
79
+        $compiler->raw(sprintf('->%s', $method));
80
+
81
+        return $this;
83 82
     }
84 83
 
85 84
     private function compileBlockArguments(Twig_Compiler $compiler)