From f96d8d8219e03ec31fff075309e484f1f1e1220a Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 27 Sep 2011 14:17:34 +0200 Subject: [PATCH] added an optimization for the parent() function --- CHANGELOG | 1 + lib/Twig/Node/Expression/Parent.php | 21 +++++++++++++++------ lib/Twig/NodeVisitor/Optimizer.php | 8 ++++++-- test/Twig/Tests/NodeVisitor/OptimizerTest.php | 13 +++++++++++++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 41b2da3..89322d1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.3.0 + * added an optimization for the parent() function * added cache reloading when auto_reload is true and an extension has been modified * added the possibility to force the escaping of a string already marked as safe (instance of Twig_Markup) * allowed empty templates to be used as traits diff --git a/lib/Twig/Node/Expression/Parent.php b/lib/Twig/Node/Expression/Parent.php index 5689fe4..ea97349 100644 --- a/lib/Twig/Node/Expression/Parent.php +++ b/lib/Twig/Node/Expression/Parent.php @@ -20,7 +20,7 @@ class Twig_Node_Expression_Parent extends Twig_Node_Expression { public function __construct($name, $lineno, $tag = null) { - parent::__construct(array(), array('name' => $name), $lineno, $tag); + parent::__construct(array(), array('output' => false, 'name' => $name), $lineno, $tag); } /** @@ -30,10 +30,19 @@ class Twig_Node_Expression_Parent extends Twig_Node_Expression */ public function compile(Twig_Compiler $compiler) { - $compiler - ->raw("\$this->renderParentBlock(") - ->string($this->getAttribute('name')) - ->raw(", \$context, \$blocks)") - ; + if ($this->getAttribute('output')) { + $compiler + ->addDebugInfo($this) + ->write("\$this->displayParentBlock(") + ->string($this->getAttribute('name')) + ->raw(", \$context, \$blocks);\n") + ; + } else { + $compiler + ->raw("\$this->renderParentBlock(") + ->string($this->getAttribute('name')) + ->raw(", \$context, \$blocks)") + ; + } } } diff --git a/lib/Twig/NodeVisitor/Optimizer.php b/lib/Twig/NodeVisitor/Optimizer.php index 3679746..d08e902 100644 --- a/lib/Twig/NodeVisitor/Optimizer.php +++ b/lib/Twig/NodeVisitor/Optimizer.php @@ -75,14 +75,18 @@ class Twig_NodeVisitor_Optimizer implements Twig_NodeVisitorInterface } /** - * Replaces "echo $this->renderBlock()" with "$this->displayBlock()". + * Replaces "echo $this->render(Parent)Block()" with "$this->display(Parent)Block()". * * @param Twig_NodeInterface $node A Node * @param Twig_Environment $env The current Twig environment */ protected function optimizeRenderBlock($node, $env) { - if ($node instanceof Twig_Node_Print && $node->getNode('expr') instanceof Twig_Node_Expression_BlockReference) { + if (!$node instanceof Twig_Node_Print) { + return $node; + } + + if ($node->getNode('expr') instanceof Twig_Node_Expression_BlockReference || $node->getNode('expr') instanceof Twig_Node_Expression_Parent) { $node->getNode('expr')->setAttribute('output', true); return $node->getNode('expr'); diff --git a/test/Twig/Tests/NodeVisitor/OptimizerTest.php b/test/Twig/Tests/NodeVisitor/OptimizerTest.php index d2d9b77..e0f3453 100644 --- a/test/Twig/Tests/NodeVisitor/OptimizerTest.php +++ b/test/Twig/Tests/NodeVisitor/OptimizerTest.php @@ -23,6 +23,19 @@ class Twig_Tests_NodeVisitor_OptimizerTest extends PHPUnit_Framework_TestCase $this->assertTrue($node->getAttribute('output')); } + public function testRenderParentBlockOptimizer() + { + $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); + $env->addExtension(new Twig_Extension_Optimizer()); + + $stream = $env->parse($env->tokenize('{% extends "foo" %}{% block content %}{{ parent() }}{% endblock %}', 'index')); + + $node = $stream->getNode('blocks')->getNode('content')->getNode('body'); + + $this->assertInstanceOf('Twig_Node_Expression_Parent', $node); + $this->assertTrue($node->getAttribute('output')); + } + public function testRenderVariableBlockOptimizer() { $env = new Twig_Environment(new Twig_Loader_String(), array('cache' => false, 'autoescape' => false)); -- 1.7.2.5