Skip to content

Commit

Permalink
feature #4553 Add ForElseNode (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.x branch.

Discussion
----------

Add ForElseNode

Replaces #4473 for 3.x

Commits
-------

6097675 Add ForElseNode
  • Loading branch information
fabpot committed Jan 24, 2025
2 parents 17b35c8 + 6097675 commit c542deb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
41 changes: 41 additions & 0 deletions src/Node/ForElseNode.php
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/
#[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")
;
}
}
14 changes: 7 additions & 7 deletions src/Node/ForNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public function __construct(AssignContextVariable $keyTarget, AssignContextVaria
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;
Expand Down Expand Up @@ -93,13 +99,7 @@ public function compile(Compiler $compiler): void
;

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");
Expand Down
3 changes: 2 additions & 1 deletion src/TokenParser/ForTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,7 +43,7 @@ public function parse(Token $token): Node
$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;
}
Expand Down
7 changes: 5 additions & 2 deletions tests/Node/ForTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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;
Expand All @@ -36,7 +37,7 @@ public function testConstructor()
$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'));
Expand Down Expand Up @@ -159,7 +160,7 @@ public static function provideTests(): iterable
$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);

Expand Down Expand Up @@ -193,7 +194,9 @@ public static function provideTests(): iterable
\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];
}
}
// line 5
if (!\$context['_iterated']) {
// line 6
yield $fooGetter;
}
\$_parent = \$context['_parent'];
Expand Down

0 comments on commit c542deb

Please sign in to comment.