Fetch the escaper runtime once in the constructor of templates that escape

This commit is contained in:
Fabien Potencier
2026-09-25 08:18:29 +02:00
parent 9e198d7637
commit 3417f483bf
7 changed files with 206 additions and 4 deletions
+12 -1
View File
@@ -74,6 +74,7 @@ final class ModuleNode extends Node implements CoercesChildrenToStringInterface
'index' => null,
'embedded_templates' => $embeddedTemplates,
'strategy' => false,
'escaper' => false,
], 1);
// populate the template name of all node children
@@ -201,8 +202,14 @@ final class ModuleNode extends Node implements CoercesChildrenToStringInterface
->write("/**\n")
->write(" * @var array<string, MacroNamespace>\n")
->write(" */\n")
->write("private array \$macros = [];\n\n")
->write("private array \$macros = [];\n")
;
if ($this->getAttribute('escaper')) {
$compiler->write("private \\Twig\\Runtime\\EscaperRuntime \$escaper;\n");
}
$compiler->raw("\n");
}
protected function compileConstructor(Compiler $compiler): void
@@ -215,6 +222,10 @@ final class ModuleNode extends Node implements CoercesChildrenToStringInterface
->write("\$this->source = \$this->getSourceContext();\n\n")
;
if ($this->getAttribute('escaper')) {
$compiler->write("\$this->escaper = \$env->getRuntime('Twig\\Runtime\\EscaperRuntime');\n\n");
}
// parent
if (!$this->hasNode('parent')) {
$compiler->write("\$this->parent = false;\n\n");