mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-13 19:06:40 +00:00
adding support for the ?? operator
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
@@ -261,6 +261,7 @@ class Twig_Extension_Core extends Twig_Extension
|
||||
'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),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user