Use loop() to recurse instead of loop.recurse()

This commit is contained in:
Fabien Potencier
2024-07-22 09:41:43 +02:00
parent e95d6b2f21
commit 8c5dd39b9b
4 changed files with 13 additions and 4 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
# 4.0.0 (2024-XX-XX)
* Add support for recursive loops (via `loop.recurse()`)
* Add support for recursive loops (via the `loop()` function)
* Add `loop.changed`, `loop.previous`, `loop.next`, and `loop.cycle` variables
* Make `loop.last` always available (even for non-countable iterators)
* Change the compilation of `for` loops to throw an exception when a `loop.*` variable is not defined
+9
View File
@@ -460,6 +460,15 @@ class ExpressionParser
}
return new GetAttrExpression($args->getNode('0'), $args->getNode('1'), \count($args) > 2 ? $args->getNode('2') : null, Template::ANY_CALL, $line);
case 'loop':
$args = $this->parseArguments();
if (\count($args) < 1) {
throw new SyntaxError('The "loop" function takes an iterator as an argument.', $line, $this->parser->getStream()->getSourceContext());
}
$recurseArgs = new ArrayExpression([], $line);
$recurseArgs->addElement($args->getNode('0'));
return new GetAttrExpression(new NameExpression('loop', $line), new ConstantExpression('__invoke', $line), $recurseArgs, Template::METHOD_CALL, $line);
default:
if (null !== $alias = $this->parser->getImportedSymbol('function', $name)) {
$arguments = new ArrayExpression([], $line);
+2 -2
View File
@@ -43,14 +43,14 @@ final class ForNodeVisitor implements NodeVisitorInterface
return $node;
}
// We look for exactly {{ loop.recurse(...) }}
// We look for exactly {{ loop.__invoke(...) }}
$exprNode = $node->getNode('expr');
if (
$exprNode instanceof GetAttrExpression
&& $exprNode->getNode('node') instanceof NameExpression
&& 'loop' === $exprNode->getNode('node')->getAttribute('name')
&& $exprNode->getNode('attribute') instanceof ConstantExpression
&& 'recurse' === $exprNode->getNode('attribute')->getAttribute('value')
&& '__invoke' === $exprNode->getNode('attribute')->getAttribute('value')
) {
$exprNode->setAttribute('is_generator', true);
}
+1 -1
View File
@@ -103,7 +103,7 @@ final class LoopContext
return $values[$this->getIndex0() % count($values)];
}
public function recurse($iterator): \Generator
public function __invoke($iterator): \Generator
{
yield from ($this->recurseFunc)(new LoopIterator($iterator), $this->parent, $this->blocks, $this->recurseFunc, $this->depth + 1);
}