From 2e291455338b35307c087477726f11181bda95b0 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 30 Dec 2010 11:58:28 +0100 Subject: [PATCH] fixed ternary operator when used for the default filter --- lib/Twig/Node/Expression/Filter.php | 2 ++ test/Twig/Tests/Fixtures/filters/default.test | 2 ++ 2 files changed, 4 insertions(+), 0 deletions(-) diff --git a/lib/Twig/Node/Expression/Filter.php b/lib/Twig/Node/Expression/Filter.php index 9cc00fc..8181215 100644 --- a/lib/Twig/Node/Expression/Filter.php +++ b/lib/Twig/Node/Expression/Filter.php @@ -29,6 +29,7 @@ class Twig_Node_Expression_Filter extends Twig_Node_Expression // is a name (like obj) or an attribute (like obj.attr) // In such a case, it's compiled to {{ obj is defined ? obj|default('bar') : 'bar' }} if ('default' === $name && ($this->getNode('node') instanceof Twig_Node_Expression_Name || $this->getNode('node') instanceof Twig_Node_Expression_GetAttr)) { + $compiler->raw('('); if ($this->getNode('node') instanceof Twig_Node_Expression_Name) { $testMap = $compiler->getEnvironment()->getTests(); $compiler @@ -45,6 +46,7 @@ class Twig_Node_Expression_Filter extends Twig_Node_Expression $this->compileFilter($compiler, $filter); $compiler->raw(' : '); $compiler->subcompile($this->getNode('arguments')->getNode(0)); + $compiler->raw(')'); } else { $this->compileFilter($compiler, $filter); } diff --git a/test/Twig/Tests/Fixtures/filters/default.test b/test/Twig/Tests/Fixtures/filters/default.test index 0c975dd..ad1a338 100644 --- a/test/Twig/Tests/Fixtures/filters/default.test +++ b/test/Twig/Tests/Fixtures/filters/default.test @@ -4,9 +4,11 @@ {{ foo|default('bar') }} {{ bar|default('foo') }} {{ not_defined|default('bar') }} +{{ 'a' ~ foo|default('b') }} --DATA-- return array('foo' => null, 'bar' => 'bar') --EXPECT-- bar bar bar +ab -- 1.7.2.5