diff --git a/src/Node/ForNode.php b/src/Node/ForNode.php index d8af8962418..122063105b5 100644 --- a/src/Node/ForNode.php +++ b/src/Node/ForNode.php @@ -101,7 +101,11 @@ public function compile(Compiler $compiler): void $compiler->write("\$_parent = \$context['_parent'];\n"); // remove some "private" loop variables (needed for nested loops) - $compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\''.$this->getNode('key_target')->getAttribute('name').'\'], $context[\''.$this->getNode('value_target')->getAttribute('name').'\'], $context[\'_parent\'], $context[\'loop\']);'."\n"); + $compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\''.$this->getNode('key_target')->getAttribute('name').'\'], $context[\''.$this->getNode('value_target')->getAttribute('name').'\'], $context[\'_parent\']'); + if ($this->getAttribute('with_loop')) { + $compiler->raw(', $context[\'loop\']'); + } + $compiler->raw(");\n"); // keep the values set in the inner context for variables defined in the outer context $compiler->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n"); diff --git a/tests/Node/ForTest.php b/tests/Node/ForTest.php index 3ca4b22a304..5d59ae80471 100644 --- a/tests/Node/ForTest.php +++ b/tests/Node/ForTest.php @@ -62,7 +62,7 @@ public function getTests() yield {$this->getVariableGetter('foo')}; } \$_parent = \$context['_parent']; -unset(\$context['_seq'], \$context['_iterated'], \$context['key'], \$context['item'], \$context['_parent'], \$context['loop']); +unset(\$context['_seq'], \$context['_iterated'], \$context['key'], \$context['item'], \$context['_parent']); \$context = array_intersect_key(\$context, \$_parent) + \$_parent; EOF ];