Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
adding support for the ?? operator
  • Loading branch information
fabpot committed Jan 25, 2016
1 parent 67fd42b commit cdd57f3
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,5 +1,6 @@
* 1.23.4 (2016-XX-XX)

* adding support for the ?? operator
* fixed the defined test when used on a constant, a map, or a sequence
* undeprecated _self (should only be used to get the template name, not the template instance)
* fixed parsing on PHP7
Expand Down
7 changes: 7 additions & 0 deletions doc/templates.rst
Expand Up @@ -808,6 +808,13 @@ The following operators don't fit into any of the other categories:
{{ foo ?: 'no' }} is the same as {{ foo ? foo : 'no' }}
{{ foo ? 'yes' }} is the same as {{ foo ? 'yes' : '' }}
* ``??``: The null-coalescing operator:

.. code-block:: jinja
{# returns the value of foo if it is defined and not null, 'no' otherwise #}
{{ foo ?? 'no' }}
String Interpolation
~~~~~~~~~~~~~~~~~~~~

Expand Down
1 change: 1 addition & 0 deletions lib/Twig/Extension/Core.php
Expand Up @@ -261,6 +261,7 @@ public function getOperators()
'is' => array('precedence' => 100, 'callable' => array($this, 'parseTestExpression'), 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
'is not' => array('precedence' => 100, 'callable' => array($this, 'parseNotTestExpression'), 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
'**' => array('precedence' => 200, 'class' => 'Twig_Node_Expression_Binary_Power', 'associativity' => Twig_ExpressionParser::OPERATOR_RIGHT),
'??' => array('precedence' => 300, 'class' => 'Twig_Node_Expression_NullCoalesce', 'associativity' => Twig_ExpressionParser::OPERATOR_RIGHT),
),
);
}
Expand Down
23 changes: 23 additions & 0 deletions lib/Twig/Node/Expression/NullCoalesce.php
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Twig_Node_Expression_NullCoalesce extends Twig_Node_Expression_Conditional
{
public function __construct(Twig_NodeInterface $left, Twig_NodeInterface $right, $lineno)
{
$test = new Twig_Node_Expression_Binary_And(
new Twig_Node_Expression_Test_Defined(clone $left, 'defined', new Twig_Node(), $left->getLine()),
new Twig_Node_Expression_Unary_Not(new Twig_Node_Expression_Test_Null($left, 'null', new Twig_Node(), $left->getLine()), $left->getLine()),
$left->getLine()
);

parent::__construct($test, $left, $right, $lineno);
}
}
30 changes: 30 additions & 0 deletions test/Twig/Tests/Fixtures/tests/null_coalesce.test
@@ -0,0 +1,30 @@
--TEST--
Twig supports the ?? operator
--TEMPLATE--
{{ 'OK' ?? 'KO' }}
{{ null ?? 'OK' }}
{{ bar ?? 'KO' }}
{{ baz ?? 'OK' }}
{{ foo.bar ?? 'KO' }}
{{ foo.missing ?? 'OK' }}
{{ foo.bar.baz.missing ?? 'OK' }}
{{ foo['bar'] ?? 'KO' }}
{{ foo['missing'] ?? 'OK' }}
{{ nope ?? nada ?? 'OK' }}
{{ 1 + nope ?? nada ?? 2 }}
{{ 1 + nope ?? 3 + nada ?? 2 }}
--DATA--
return array('bar' => 'OK', 'foo' => array('bar' => 'OK'))
--EXPECT--
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
3
6

0 comments on commit cdd57f3

Please sign in to comment.