From 3df2c96b296bbcb6b713bf0c04d78f8f7fd0d2d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Tue, 6 May 2025 04:29:53 +0200 Subject: [PATCH] [TwigComponent] Fix `loadTemplate` deprecation for Twig >= 3.21 Fix #2710 cf https://github.com/twigphp/Twig/pull/4583 --- src/TwigComponent/src/Twig/ComponentNode.php | 34 ++++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/TwigComponent/src/Twig/ComponentNode.php b/src/TwigComponent/src/Twig/ComponentNode.php index df735079570..96ef9c7ca0b 100644 --- a/src/TwigComponent/src/Twig/ComponentNode.php +++ b/src/TwigComponent/src/Twig/ComponentNode.php @@ -19,6 +19,7 @@ use Twig\Node\Expression\AbstractExpression; use Twig\Node\Node; use Twig\Node\NodeOutputInterface; +use Twig\Template; /** * @author Fabien Potencier @@ -154,16 +155,29 @@ public function compile(Compiler $compiler): void if ($useYield) { $compiler->write('yield from '); } - $compiler - ->write('$this->loadTemplate(') - ->string($this->getAttribute('embedded_template')) - ->raw(', ') - ->repr($this->getTemplateName()) - ->raw(', ') - ->repr($this->getTemplateLine()) - ->raw(', ') - ->string($this->getAttribute('embedded_index')) - ->raw(')'); + + // Support for Twig ^3.21 + if (method_exists(Template::class, 'load')) { + $compiler + ->write('$this->load(') + ->string($this->getAttribute('embedded_template')) + ->raw(', ') + ->repr($this->getTemplateLine()) + ->raw(', ') + ->string($this->getAttribute('embedded_index')) + ->raw(')'); + } else { + $compiler + ->write('$this->loadTemplate(') + ->string($this->getAttribute('embedded_template')) + ->raw(', ') + ->repr($this->getTemplateName()) + ->raw(', ') + ->repr($this->getTemplateLine()) + ->raw(', ') + ->string($this->getAttribute('embedded_index')) + ->raw(')'); + } if ($useYield) { $compiler->raw('->unwrap()->yield(');