* 1.2.0
+ * added an optimization for the set tag when used to capture a large chunk of static text
* changed name regex to match PHP one "[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*" (works for blocks, tags, functions, filters, and macros)
* removed the possibility to use the "extends" tag from a block
* added "if" modifier support to "for" loops
*/
public function compile(Twig_Compiler $compiler)
{
+ /*
+ * Optimizes the node when capture is used for a large block of text.
+ *
+ * {% set foo %}foo{% endset %} is compiled to $context['foo'] = new Twig_Markup("foo");
+ */
+ $safe = false;
+ $values = $this->getNode('values');
+ if ($this->getAttribute('capture') && $values instanceof Twig_Node_Text) {
+ $this->setNode('values', new Twig_Node_Expression_Constant($values->getAttribute('data'), $values->getLine()));
+ $this->setAttribute('capture', false);
+ $safe = true;
+ }
+
$compiler->addDebugInfo($this);
if (count($this->getNode('names')) > 1) {
}
$compiler->raw(')');
} else {
- $compiler->subcompile($this->getNode('values'));
+ if ($safe) {
+ $compiler
+ ->raw("new Twig_Markup(")
+ ->subcompile($this->getNode('values'))
+ ->raw(")")
+ ;
+ } else {
+ $compiler->subcompile($this->getNode('values'));
+ }
}
}
EOF
);
+ $names = new Twig_Node(array(new Twig_Node_Expression_AssignName('foo', 0)), array(), 0);
+ $values = new Twig_Node_Text('foo', 0);
+ $node = new Twig_Node_Set(true, $names, $values, 0);
+ $tests[] = array($node, '$context[\'foo\'] = new Twig_Markup("foo");');
+
$names = new Twig_Node(array(new Twig_Node_Expression_AssignName('foo', 0), new Twig_Node_Expression_AssignName('bar', 0)), array(), 0);
$values = new Twig_Node(array(new Twig_Node_Expression_Constant('foo', 0), new Twig_Node_Expression_Name('bar', 0)), array(), 0);
$node = new Twig_Node_Set(false, $names, $values, 0);