From: Fabien Potencier Date: Wed, 14 Nov 2012 06:40:53 +0000 (+0100) Subject: added support for extended ternary operator syntaxes (closes #134) X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=f85076b091c6b6a2f9e14276a436724d23a1217e;p=web%2Fkonrad%2Ftwig.git added support for extended ternary operator syntaxes (closes #134) --- diff --git a/CHANGELOG b/CHANGELOG index 51121d1..0e7b85b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ * 1.11.2 (2012-XX-XX) - * n/a + * added support for extended ternary operator syntaxes * 1.11.1 (2012-11-11) diff --git a/doc/templates.rst b/doc/templates.rst index 104dcad..64761db 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -659,6 +659,9 @@ tests. Other Operators ~~~~~~~~~~~~~~~ +.. versionadded:: 1.11.2 + Support for the extended ternary operator was added in Twig 1.11.2. + The following operators are very useful but don't fit into any of the other categories: @@ -674,7 +677,15 @@ categories: * ``.``, ``[]``: Gets an attribute of an object. -* ``?:``: The PHP ternary operator: ``{{ foo ? 'yes' : 'no' }}`` +* ``?:``: The ternary operator: + + .. code-block:: jinja + + {{ foo ? 'yes' : 'no' }} + + {# as of Twig 1.11.2 #} + {{ foo ?: 'no' }} == {{ foo ? foo : 'no' }} + {{ foo ? 'yes' }} == {{ foo ? 'yes' : '' }} String Interpolation ~~~~~~~~~~~~~~~~~~~~ diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index 3309bb5..62b90ea 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -89,9 +89,19 @@ class Twig_ExpressionParser { while ($this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '?')) { $this->parser->getStream()->next(); - $expr2 = $this->parseExpression(); - $this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'The ternary operator must have a default value'); - $expr3 = $this->parseExpression(); + if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, ':')) { + $expr2 = $this->parseExpression(); + if ($this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, ':')) { + $this->parser->getStream()->next(); + $expr3 = $this->parseExpression(); + } else { + $expr3 = new Twig_Node_Expression_Constant('', $this->parser->getCurrentToken()->getLine()); + } + } else { + $this->parser->getStream()->next(); + $expr2 = $expr; + $expr3 = $this->parseExpression(); + } $expr = new Twig_Node_Expression_Conditional($expr, $expr2, $expr3, $this->parser->getCurrentToken()->getLine()); } diff --git a/test/Twig/Tests/Fixtures/expressions/ternary_operator_noelse.test b/test/Twig/Tests/Fixtures/expressions/ternary_operator_noelse.test new file mode 100644 index 0000000..fdc660f --- /dev/null +++ b/test/Twig/Tests/Fixtures/expressions/ternary_operator_noelse.test @@ -0,0 +1,10 @@ +--TEST-- +Twig supports the ternary operator +--TEMPLATE-- +{{ 1 ? 'YES' }} +{{ 0 ? 'YES' }} +--DATA-- +return array() +--EXPECT-- +YES + diff --git a/test/Twig/Tests/Fixtures/expressions/ternary_operator_nothen.test b/test/Twig/Tests/Fixtures/expressions/ternary_operator_nothen.test new file mode 100644 index 0000000..9057e83 --- /dev/null +++ b/test/Twig/Tests/Fixtures/expressions/ternary_operator_nothen.test @@ -0,0 +1,10 @@ +--TEST-- +Twig supports the ternary operator +--TEMPLATE-- +{{ 'YES' ?: 'NO' }} +{{ 0 ?: 'NO' }} +--DATA-- +return array() +--EXPECT-- +YES +NO