Simplified Twig_Node_Include
authorMartin Hasoň <martin.hason@gmail.com>
Wed, 30 Jul 2014 21:27:46 +0000 (23:27 +0200)
committerMartin Hasoň <martin.hason@gmail.com>
Thu, 31 Jul 2014 07:18:56 +0000 (09:18 +0200)
lib/Twig/Node/Include.php
test/Twig/Tests/Node/IncludeTest.php

index 860aedf..e11cfc1 100644 (file)
@@ -60,40 +60,26 @@ class Twig_Node_Include extends Twig_Node implements Twig_NodeOutputInterface
 
     protected function addGetTemplate(Twig_Compiler $compiler)
     {
-        if ($this->getNode('expr') instanceof Twig_Node_Expression_Constant) {
-            $compiler
-                ->write("\$this->env->loadTemplate(")
-                ->subcompile($this->getNode('expr'))
-                ->raw(")")
-            ;
-        } else {
-            $compiler
-                ->write("\$template = \$this->env->resolveTemplate(")
-                ->subcompile($this->getNode('expr'))
-                ->raw(");\n")
-                ->write('$template')
-            ;
-        }
+        $method = $this->getNode('expr') instanceof Twig_Node_Expression_Constant ? 'loadTemplate' : 'resolveTemplate';
+        $compiler
+            ->write(sprintf('$this->env->%s(', $method))
+            ->subcompile($this->getNode('expr'))
+            ->raw(')')
+        ;
     }
 
     protected function addTemplateArguments(Twig_Compiler $compiler)
     {
-        if (false === $this->getAttribute('only')) {
-            if (null === $this->getNode('variables')) {
-                $compiler->raw('$context');
-            } else {
-                $compiler
-                    ->raw('array_merge($context, ')
-                    ->subcompile($this->getNode('variables'))
-                    ->raw(')')
-                ;
-            }
+        if (null === $this->getNode('variables')) {
+            $compiler->raw(false === $this->getAttribute('only') ? '$context' : 'array()');
+        } elseif (false === $this->getAttribute('only')) {
+            $compiler
+                ->raw('array_merge($context, ')
+                ->subcompile($this->getNode('variables'))
+                ->raw(')')
+            ;
         } else {
-            if (null === $this->getNode('variables')) {
-                $compiler->raw('array()');
-            } else {
-                $compiler->subcompile($this->getNode('variables'));
-            }
+            $compiler->subcompile($this->getNode('variables'));
         }
     }
 }
index e8aa326..9afecef 100644 (file)
@@ -59,8 +59,7 @@ EOF
         $node = new Twig_Node_Include($expr, null, false, false, 1);
         $tests[] = array($node, <<<EOF
 // line 1
-\$template = \$this->env->resolveTemplate(((true) ? ("foo") : ("foo")));
-\$template->display(\$context);
+\$this->env->resolveTemplate(((true) ? ("foo") : ("foo")))->display(\$context);
 EOF
         );