mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-10 01:16:52 +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)
|
||||
{
|
||||
$test = new AndBinary(
|
||||
new DefinedTest(clone $left, 'defined', new Node(), $left->getTemplateLine()),
|
||||
new NotUnary(new NullTest($left, 'null', new Node(), $left->getTemplateLine()), $left->getTemplateLine()),
|
||||
$left->getTemplateLine()
|
||||
);
|
||||
$test = 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
|
||||
if (!$left instanceof BlockReferenceExpression) {
|
||||
$test = new AndBinary(
|
||||
$test,
|
||||
new NotUnary(new NullTest($left, 'null', new Node(), $left->getTemplateLine()), $left->getTemplateLine()),
|
||||
$left->getTemplateLine()
|
||||
);
|
||||
}
|
||||
|
||||
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