Skip to content

Commit e122fa3

Browse files
committed
[TwigComponent] Add compat with Twig 3.21 "load"/"loadTemplate"
cf https://github.com/twigphp/Twig/pull/4583/files
1 parent 6d2daab commit e122fa3

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

Diff for: src/TwigComponent/src/ComponentRenderer.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent;
1818
use Symfony\UX\TwigComponent\Event\PreRenderEvent;
1919
use Twig\Environment;
20+
use Twig\Template;
2021

2122
/**
2223
* @author Kevin Bond <[email protected]>
@@ -70,11 +71,14 @@ public function render(MountedComponent $mounted): string
7071
}
7172

7273
try {
73-
return $this->twig->loadTemplate(
74-
$this->templateClasses[$template = $event->getTemplate()] ??= $this->twig->getTemplateClass($template),
75-
$template,
76-
$templateIndex,
77-
)->render($variables);
74+
if (method_exists(Template::class, 'load')) {
75+
return $this->twig->load($event->getTemplate())->render($variables);
76+
}
77+
78+
// Environment::loadTemplate is deprecated since Twig 3.21
79+
$templateClass = $this->templateClasses[$template = $event->getTemplate()] ??= $this->twig->getTemplateClass($template, $templateIndex);
80+
81+
return $this->twig->loadTemplate($templateClass, $template, $templateIndex)->render($variables);
7882
} finally {
7983
$mounted = $this->componentStack->pop();
8084

Diff for: src/TwigComponent/src/Twig/ComponentNode.php

+23-10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Twig\Node\Expression\AbstractExpression;
2020
use Twig\Node\Node;
2121
use Twig\Node\NodeOutputInterface;
22+
use Twig\Template;
2223

2324
/**
2425
* @author Fabien Potencier <[email protected]>
@@ -154,17 +155,29 @@ public function compile(Compiler $compiler): void
154155
if ($useYield) {
155156
$compiler->write('yield from ');
156157
}
157-
$compiler
158-
->write('$this->loadTemplate(')
159-
->string($this->getAttribute('embedded_template'))
160-
->raw(', ')
161-
->repr($this->getTemplateName())
162-
->raw(', ')
163-
->repr($this->getTemplateLine())
164-
->raw(', ')
165-
->string($this->getAttribute('embedded_index'))
166-
->raw(')');
167158

159+
if (method_exists(Template::class, 'load')) {
160+
$compiler
161+
->write('$this->load(')
162+
->string($this->getAttribute('embedded_template'))
163+
->raw(', ')
164+
->raw($this->getTemplateLine())
165+
->raw(', ')
166+
->string($this->getAttribute('embedded_index'))
167+
->raw(')');
168+
} else {
169+
// Environment::loadTemplate is deprecated since Twig 3.21
170+
$compiler
171+
->write('$this->loadTemplate(')
172+
->string($this->getAttribute('embedded_template'))
173+
->raw(', ')
174+
->repr($this->getTemplateName())
175+
->raw(', ')
176+
->repr($this->getTemplateLine())
177+
->raw(', ')
178+
->string($this->getAttribute('embedded_index'))
179+
->raw(')');
180+
}
168181
if ($useYield) {
169182
$compiler->raw('->unwrap()->yield(');
170183
} else {

Diff for: src/TwigComponent/tests/Integration/ComponentExtensionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ public function testComponentWithPropsFromTemplateAndClass(): void
425425
public function testComponentWithConflictBetweenPropsFromTemplateAndClass(): void
426426
{
427427
$this->expectException(RuntimeError::class);
428-
$this->expectExceptionMessage('Cannot define prop "name" in template "components/Conflict.html.twig". Property already defined in component class "Symfony\UX\TwigComponent\Tests\Fixtures\Component\Conflict".');
428+
$this->expectExceptionMessage('Cannot define prop "name" in template "components/Conflict.html.twig"');
429429

430430
self::getContainer()->get(Environment::class)->render('component_with_conflict_between_props_from_template_and_class.html.twig');
431431
}

Diff for: src/TwigComponent/tests/Integration/EmbeddedComponentTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function testAccessingTheHierarchyTooHighThrowsAnException(): void
162162
{
163163
// Twig renamed "array" into "sequence" in 3.11
164164
$this->expectExceptionMessage('Key "$this" for ');
165-
$this->expectExceptionMessage('with keys "app, __embedded" does not exist.');
165+
$this->expectExceptionMessage('with keys "app, __embedded" does not exist');
166166
self::render('embedded_component_hierarchy_exception.html.twig');
167167
}
168168

0 commit comments

Comments
 (0)