added support for extended ternary operator syntaxes (closes #134)
authorFabien Potencier <fabien.potencier@gmail.com>
Wed, 14 Nov 2012 06:40:53 +0000 (07:40 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Wed, 14 Nov 2012 06:40:53 +0000 (07:40 +0100)
CHANGELOG
doc/templates.rst
lib/Twig/ExpressionParser.php
test/Twig/Tests/Fixtures/expressions/ternary_operator_noelse.test [new file with mode: 0644]
test/Twig/Tests/Fixtures/expressions/ternary_operator_nothen.test [new file with mode: 0644]

index 51121d1..0e7b85b 100644 (file)
--- 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)
 
index 104dcad..64761db 100644 (file)
@@ -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
 ~~~~~~~~~~~~~~~~~~~~
index 3309bb5..62b90ea 100644 (file)
@@ -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 (file)
index 0000000..fdc660f
--- /dev/null
@@ -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 (file)
index 0000000..9057e83
--- /dev/null
@@ -0,0 +1,10 @@
+--TEST--
+Twig supports the ternary operator
+--TEMPLATE--
+{{ 'YES' ?: 'NO' }}
+{{ 0 ?: 'NO' }}
+--DATA--
+return array()
+--EXPECT--
+YES
+NO