optimized parent template creation when the template does not use dynamic inheritance

This commit is contained in:
Fabien Potencier
2012-03-30 21:05:14 +02:00
parent 6e2fd68e7b
commit 1e9a1ae0be
5 changed files with 27 additions and 9 deletions
+1 -1
View File
@@ -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)
+19 -6
View File
@@ -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
+5
View File
@@ -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;
+1 -1
View File
@@ -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()
+1 -1
View File
@@ -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() {