Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/TwigComponent/src/ComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent;
use Symfony\UX\TwigComponent\Event\PreRenderEvent;
use Twig\Environment;
use Twig\Template;

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

try {
return $this->twig->loadTemplate(
$this->templateClasses[$template = $event->getTemplate()] ??= $this->twig->getTemplateClass($template),
$template,
$templateIndex,
)->render($variables);
if (method_exists(Template::class, 'load')) {
return $this->twig->load($event->getTemplate())->render($variables);
}

// Environment::loadTemplate is deprecated since Twig 3.21
$templateClass = $this->templateClasses[$template = $event->getTemplate()] ??= $this->twig->getTemplateClass($template, $templateIndex);

return $this->twig->loadTemplate($templateClass, $template, $templateIndex)->render($variables);
} finally {
$mounted = $this->componentStack->pop();

Expand Down
33 changes: 23 additions & 10 deletions src/TwigComponent/src/Twig/ComponentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Node;
use Twig\Node\NodeOutputInterface;
use Twig\Template;

/**
* @author Fabien Potencier <[email protected]>
Expand Down Expand Up @@ -154,17 +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(')');

if (method_exists(Template::class, 'load')) {
$compiler
->write('$this->load(')
->string($this->getAttribute('embedded_template'))
->raw(', ')
->raw($this->getTemplateLine())
->raw(', ')
->string($this->getAttribute('embedded_index'))
->raw(')');
} else {
// Environment::loadTemplate is deprecated since Twig 3.21
$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(');
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public function testComponentWithPropsFromTemplateAndClass(): void
public function testComponentWithConflictBetweenPropsFromTemplateAndClass(): void
{
$this->expectException(RuntimeError::class);
$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".');
$this->expectExceptionMessage('Cannot define prop "name" in template "components/Conflict.html.twig"');

self::getContainer()->get(Environment::class)->render('component_with_conflict_between_props_from_template_and_class.html.twig');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function testAccessingTheHierarchyTooHighThrowsAnException(): void
{
// Twig renamed "array" into "sequence" in 3.11
$this->expectExceptionMessage('Key "$this" for ');
$this->expectExceptionMessage('with keys "app, __embedded" does not exist.');
$this->expectExceptionMessage('with keys "app, __embedded" does not exist');
self::render('embedded_component_hierarchy_exception.html.twig');
}

Expand Down
Loading