Permalink
Browse files

added an optimization for the parent() function

  • Loading branch information...
1 parent d9d38d6 commit f96d8d8219e03ec31fff075309e484f1f1e1220a @fabpot committed Sep 27, 2011
View
@@ -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
@@ -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 @@ public function __construct($name, $lineno, $tag = null)
*/
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)")
+ ;
+ }
}
}
@@ -75,14 +75,18 @@ public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
}
/**
- * 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');
@@ -23,6 +23,19 @@ public function testRenderBlockOptimizer()
$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));

0 comments on commit f96d8d8

Please sign in to comment.