mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 11:27:00 +00:00
Optimize block('foo') ?? 'bar'
This commit is contained in:
@@ -22,11 +22,15 @@ class NullCoalesceExpression extends ConditionalExpression
|
|||||||
{
|
{
|
||||||
public function __construct(\Twig_NodeInterface $left, \Twig_NodeInterface $right, $lineno)
|
public function __construct(\Twig_NodeInterface $left, \Twig_NodeInterface $right, $lineno)
|
||||||
{
|
{
|
||||||
$test = new AndBinary(
|
$test = new DefinedTest(clone $left, 'defined', new Node(), $left->getTemplateLine());
|
||||||
new DefinedTest(clone $left, 'defined', new Node(), $left->getTemplateLine()),
|
// for "block()", we don't need the null test as the return value is always a string
|
||||||
new NotUnary(new NullTest($left, 'null', new Node(), $left->getTemplateLine()), $left->getTemplateLine()),
|
if (!$left instanceof BlockReferenceExpression) {
|
||||||
$left->getTemplateLine()
|
$test = new AndBinary(
|
||||||
);
|
$test,
|
||||||
|
new NotUnary(new NullTest($left, 'null', new Node(), $left->getTemplateLine()), $left->getTemplateLine()),
|
||||||
|
$left->getTemplateLine()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
parent::__construct($test, $left, $right, $lineno);
|
parent::__construct($test, $left, $right, $lineno);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
--TEST--
|
||||||
|
Twig supports the ?? operator with blocks
|
||||||
|
--TEMPLATE--
|
||||||
|
{% block foo %}OK{% endblock %}
|
||||||
|
{{ block('foo') ?? 'KO' }}
|
||||||
|
--DATA--
|
||||||
|
return []
|
||||||
|
--EXPECT--
|
||||||
|
OKOK
|
||||||
Reference in New Issue
Block a user