* 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
{
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);
}
/**
*/
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)")
+ ;
+ }
}
}
}
/**
- * 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');
$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));