Add support for templates that do not have output Nodes

This commit is contained in:
Fabien Potencier
2024-01-11 08:22:24 +01:00
parent 72922a3612
commit 1469e6a6bc
+18
View File
@@ -342,6 +342,9 @@ final class ModuleNode extends Node
} else {
$compiler->raw("->display(\$context, array_merge(\$this->blocks, \$blocks));\n");
}
} elseif ($compiler->getEnvironment()->useYield() && !$this->hasNodeOutputNodes($this->getNode('body'))) {
// ensure at least one yield call even for templates with no output
$compiler->write("yield;\n");
}
$compiler
@@ -483,4 +486,19 @@ final class ModuleNode extends Node
throw new \LogicException('Trait templates can only be constant nodes.');
}
}
private function hasNodeOutputNodes(Node $node): bool
{
if ($node instanceof NodeOutputInterface) {
return true;
}
foreach ($node as $child) {
if ($this->hasNodeOutputNodes($child)) {
return true;
}
}
return false;
}
}