mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 20:16:45 +00:00
Add ForElseNode
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
# 3.19.0 (2025-XX-XX)
|
||||
|
||||
* Add `ForElseNode`
|
||||
* Deprecate `Twig\ExpressionParser::parseOnlyArguments()` and
|
||||
`Twig\ExpressionParser::parseArguments()` (use
|
||||
`Twig\ExpressionParser::parseNamedArguments()` instead)
|
||||
|
||||
* Fix `constant()` behavior when used with `??`
|
||||
* Add the `invoke` filter
|
||||
* Make `{}` optional for the `types` tag
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
namespace Twig\Node;
|
||||
|
||||
use Twig\Attribute\YieldReady;
|
||||
use Twig\Compiler;
|
||||
|
||||
/**
|
||||
* Represents an else node in a for loop.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
#[YieldReady]
|
||||
class ForElseNode extends Node
|
||||
{
|
||||
public function __construct(Node $body, int $lineno)
|
||||
{
|
||||
parent::__construct(['body' => $body], [], $lineno);
|
||||
}
|
||||
|
||||
public function compile(Compiler $compiler): void
|
||||
{
|
||||
$compiler
|
||||
->addDebugInfo($this)
|
||||
->write("if (!\$context['_iterated']) {\n")
|
||||
->indent()
|
||||
->subcompile($this->getNode('body'))
|
||||
->outdent()
|
||||
->write("}\n")
|
||||
;
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,12 @@ class ForNode extends Node
|
||||
trigger_deprecation('twig/twig', '3.19', \sprintf('Passing not-null to the "ifexpr" argument of the "%s" constructor is deprecated.', static::class));
|
||||
}
|
||||
|
||||
if (null !== $else && !$else instanceof ForElseNode) {
|
||||
trigger_deprecation('twig/twig', '3.19', \sprintf('Not passing an instance of "%s" to the "else" argument of the "%s" constructor is deprecated.', ForElseNode::class, static::class));
|
||||
|
||||
$else = new ForElseNode($else, $else->getTemplateLine());
|
||||
}
|
||||
|
||||
$nodes = ['key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body];
|
||||
if (null !== $else) {
|
||||
$nodes['else'] = $else;
|
||||
@@ -93,13 +99,7 @@ class ForNode extends Node
|
||||
;
|
||||
|
||||
if ($this->hasNode('else')) {
|
||||
$compiler
|
||||
->write("if (!\$context['_iterated']) {\n")
|
||||
->indent()
|
||||
->subcompile($this->getNode('else'))
|
||||
->outdent()
|
||||
->write("}\n")
|
||||
;
|
||||
$compiler->subcompile($this->getNode('else'));
|
||||
}
|
||||
|
||||
$compiler->write("\$_parent = \$context['_parent'];\n");
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
namespace Twig\TokenParser;
|
||||
|
||||
use Twig\Node\Expression\Variable\AssignContextVariable;
|
||||
use Twig\Node\ForElseNode;
|
||||
use Twig\Node\ForNode;
|
||||
use Twig\Node\Node;
|
||||
use Twig\Token;
|
||||
@@ -42,7 +43,7 @@ final class ForTokenParser extends AbstractTokenParser
|
||||
$body = $this->parser->subparse([$this, 'decideForFork']);
|
||||
if ('else' == $stream->next()->getValue()) {
|
||||
$stream->expect(Token::BLOCK_END_TYPE);
|
||||
$else = $this->parser->subparse([$this, 'decideForEnd'], true);
|
||||
$else = new ForElseNode($this->parser->subparse([$this, 'decideForEnd'], true), $stream->getCurrent()->getLine());
|
||||
} else {
|
||||
$else = null;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Twig\Tests\Node;
|
||||
|
||||
use Twig\Node\Expression\Variable\AssignContextVariable;
|
||||
use Twig\Node\Expression\Variable\ContextVariable;
|
||||
use Twig\Node\ForElseNode;
|
||||
use Twig\Node\ForNode;
|
||||
use Twig\Node\Nodes;
|
||||
use Twig\Node\PrintNode;
|
||||
@@ -36,7 +37,7 @@ class ForTest extends NodeTestCase
|
||||
$this->assertEquals($body, $node->getNode('body')->getNode('0'));
|
||||
$this->assertFalse($node->hasNode('else'));
|
||||
|
||||
$else = new PrintNode(new ContextVariable('foo', 1), 1);
|
||||
$else = new ForElseNode(new PrintNode(new ContextVariable('foo', 1), 1), 5);
|
||||
$node = new ForNode($keyTarget, $valueTarget, $seq, null, $body, $else, 1);
|
||||
$node->setAttribute('with_loop', false);
|
||||
$this->assertEquals($else, $node->getNode('else'));
|
||||
@@ -159,7 +160,7 @@ EOF
|
||||
$valueTarget = new AssignContextVariable('v', 1);
|
||||
$seq = new ContextVariable('values', 1);
|
||||
$body = new Nodes([new PrintNode(new ContextVariable('foo', 1), 1)], 1);
|
||||
$else = new PrintNode(new ContextVariable('foo', 1), 1);
|
||||
$else = new ForElseNode(new PrintNode(new ContextVariable('foo', 6), 6), 5);
|
||||
$node = new ForNode($keyTarget, $valueTarget, $seq, null, $body, $else, 1);
|
||||
$node->setAttribute('with_loop', true);
|
||||
|
||||
@@ -193,7 +194,9 @@ foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) {
|
||||
\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];
|
||||
}
|
||||
}
|
||||
// line 5
|
||||
if (!\$context['_iterated']) {
|
||||
// line 6
|
||||
yield $fooGetter;
|
||||
}
|
||||
\$_parent = \$context['_parent'];
|
||||
|
||||
Reference in New Issue
Block a user