From 632fe900005841c4e01cc3ea74cdc79f047ec9dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Wed, 30 Jul 2014 23:27:46 +0200 Subject: [PATCH] Simplified Twig_Node_Include --- lib/Twig/Node/Include.php | 44 ++++++++++------------------ test/Twig/Tests/Node/IncludeTest.php | 3 +- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/lib/Twig/Node/Include.php b/lib/Twig/Node/Include.php index 860aedfe2..e11cfc1ef 100644 --- a/lib/Twig/Node/Include.php +++ b/lib/Twig/Node/Include.php @@ -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')); } } } diff --git a/test/Twig/Tests/Node/IncludeTest.php b/test/Twig/Tests/Node/IncludeTest.php index e8aa326b3..9afecef85 100644 --- a/test/Twig/Tests/Node/IncludeTest.php +++ b/test/Twig/Tests/Node/IncludeTest.php @@ -59,8 +59,7 @@ EOF $node = new Twig_Node_Include($expr, null, false, false, 1); $tests[] = array($node, <<env->resolveTemplate(((true) ? ("foo") : ("foo"))); -\$template->display(\$context); +\$this->env->resolveTemplate(((true) ? ("foo") : ("foo")))->display(\$context); EOF );