From 1e9a1ae0bec1663a9f40b0f3b8a90b96346ef0ab Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 30 Mar 2012 21:05:14 +0200 Subject: [PATCH] optimized parent template creation when the template does not use dynamic inheritance --- CHANGELOG | 2 +- lib/Twig/Node/Module.php | 25 +++++++++++++++++++------ lib/Twig/Template.php | 5 +++++ test/Twig/Tests/Node/ModuleTest.php | 2 +- test/Twig/Tests/Node/SandboxedModuleTest.php | 2 +- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index f813f18..9b4c198 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ * 1.6.4 (2012-XX-XX) - * n/a + * optimized parent template creation when the template does not use dynamic inheritance * 1.6.3 (2012-03-22) diff --git a/lib/Twig/Node/Module.php b/lib/Twig/Node/Module.php index 8585935..eb60c42 100644 --- a/lib/Twig/Node/Module.php +++ b/lib/Twig/Node/Module.php @@ -96,7 +96,12 @@ class Twig_Node_Module extends Twig_Node $compiler->subcompile($this->getNode('body')); if (null !== $this->getNode('parent')) { - $compiler->write("\$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));\n"); + if ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) { + $compiler->write("\$this->parent"); + } else { + $compiler->write("\$this->getParent(\$context)"); + } + $compiler->raw("->display(\$context, array_merge(\$this->blocks, \$blocks));\n"); } } @@ -121,6 +126,17 @@ class Twig_Node_Module extends Twig_Node ->write("parent::__construct(\$env);\n\n") ; + // parent + if (null === $this->getNode('parent')) { + $compiler->write("\$this->parent = false;\n\n"); + } elseif ($this->getNode('parent') instanceof Twig_Node_Expression_Constant) { + $compiler + ->write("\$this->parent = \$this->env->loadTemplate(") + ->subcompile($this->getNode('parent')) + ->raw(");\n\n") + ; + } + $countTraits = count($this->getNode('traits')); if ($countTraits) { // traits @@ -166,9 +182,6 @@ class Twig_Node_Module extends Twig_Node $compiler ->outdent() ->write(");\n\n") - ; - - $compiler ->write("\$this->blocks = array_merge(\n") ->indent() ->write("\$this->traits,\n") @@ -300,7 +313,7 @@ class Twig_Node_Module extends Twig_Node ; } - public function compileDebugInfo(Twig_Compiler $compiler) + protected function compileDebugInfo(Twig_Compiler $compiler) { $compiler ->write("public function getDebugInfo()\n", "{\n") @@ -311,7 +324,7 @@ class Twig_Node_Module extends Twig_Node ; } - public function compileLoadTemplate(Twig_Compiler $compiler, $node, $var) + protected function compileLoadTemplate(Twig_Compiler $compiler, $node, $var) { if ($node instanceof Twig_Node_Expression_Constant) { $compiler diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 5fd95ef..1862064 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -20,6 +20,7 @@ abstract class Twig_Template implements Twig_TemplateInterface { static protected $cache = array(); + protected $parent; protected $parents; protected $env; protected $blocks; @@ -62,6 +63,10 @@ abstract class Twig_Template implements Twig_TemplateInterface */ public function getParent(array $context) { + if (null !== $this->parent) { + return $this->parent; + } + $parent = $this->doGetParent($context); if (false === $parent) { return false; diff --git a/test/Twig/Tests/Node/ModuleTest.php b/test/Twig/Tests/Node/ModuleTest.php index 6ca4087..4b2b2e9 100644 --- a/test/Twig/Tests/Node/ModuleTest.php +++ b/test/Twig/Tests/Node/ModuleTest.php @@ -107,7 +107,7 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template protected function doDisplay(array \$context, array \$blocks = array()) { \$context["macro"] = \$this->env->loadTemplate("foo.twig"); - \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks)); + \$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks)); } public function getTemplateName() diff --git a/test/Twig/Tests/Node/SandboxedModuleTest.php b/test/Twig/Tests/Node/SandboxedModuleTest.php index 1ed141b..f86442b 100644 --- a/test/Twig/Tests/Node/SandboxedModuleTest.php +++ b/test/Twig/Tests/Node/SandboxedModuleTest.php @@ -118,7 +118,7 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template protected function doDisplay(array \$context, array \$blocks = array()) { \$this->checkSecurity(); - \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks)); + \$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks)); } protected function checkSecurity() { -- 1.7.2.5