From: Jordi Boggiano Date: Fri, 17 Feb 2012 14:56:34 +0000 (+0100) Subject: Avoid creating unnecessary Twig_Markup instances, allows testing for falsiness of... X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=e4590d0cd8cf2d8cca4b71cce11e335d4f6a753b;p=web%2Fkonrad%2Ftwig.git Avoid creating unnecessary Twig_Markup instances, allows testing for falsiness of empty output --- diff --git a/lib/Twig/Node/Set.php b/lib/Twig/Node/Set.php index 098ed40..70bb519 100644 --- a/lib/Twig/Node/Set.php +++ b/lib/Twig/Node/Set.php @@ -67,7 +67,7 @@ class Twig_Node_Set extends Twig_Node $compiler->subcompile($this->getNode('names'), false); if ($this->getAttribute('capture')) { - $compiler->raw(" = new Twig_Markup(ob_get_clean(), \$this->env->getCharset())"); + $compiler->raw(" = ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())"); } } @@ -87,9 +87,9 @@ class Twig_Node_Set extends Twig_Node } else { if ($this->getAttribute('safe')) { $compiler - ->raw("new Twig_Markup(") + ->raw("('' === \$tmp = ") ->subcompile($this->getNode('values')) - ->raw(", \$this->env->getCharset())") + ->raw(") ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset())") ; } else { $compiler->subcompile($this->getNode('values')); diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 324410e..7c4b4a1 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -429,7 +429,7 @@ abstract class Twig_Template implements Twig_TemplateInterface // hack to be removed when macro calls are refactored if ($object instanceof Twig_TemplateInterface) { - return new Twig_Markup($ret, $this->env->getCharset()); + return $ret === '' ? '' : new Twig_Markup($ret, $this->env->getCharset()); } return $ret; diff --git a/test/Twig/Tests/Fixtures/tags/set/capture-empty.test b/test/Twig/Tests/Fixtures/tags/set/capture-empty.test new file mode 100644 index 0000000..ec657f0 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/set/capture-empty.test @@ -0,0 +1,9 @@ +--TEST-- +"set" tag block empty capture +--TEMPLATE-- +{% set foo %}{% endset %} + +{% if foo %}FAIL{% endif %} +--DATA-- +return array() +--EXPECT-- diff --git a/test/Twig/Tests/Node/SetTest.php b/test/Twig/Tests/Node/SetTest.php index 4c59854..6319fb6 100644 --- a/test/Twig/Tests/Node/SetTest.php +++ b/test/Twig/Tests/Node/SetTest.php @@ -51,14 +51,14 @@ class Twig_Tests_Node_SetTest extends Twig_Tests_Node_TestCase $tests[] = array($node, <<env->getCharset()); +\$context["foo"] = ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset()); 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", $this->env->getCharset());'); + $tests[] = array($node, '$context["foo"] = (\'\' === $tmp = "foo") ? \'\' : new Twig_Markup($tmp, $this->env->getCharset());'); $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);