From c67f5b76a3e25e344e93c3eb2ea809b166532e9f Mon Sep 17 00:00:00 2001 From: Jano Paetzold Date: Thu, 4 Dec 2025 17:41:52 +0100 Subject: [PATCH 1/2] Reproduce InteractsWithTwigComponents not being able to render blocks when variable scope is limited in tests --- .../Component/WithSlotsAndLimitedScope.php | 22 +++++++++++++++++++ .../WithSlotsAndLimitedScope.html.twig | 7 ++++++ .../Integration/ComponentFactoryTest.php | 2 +- .../Test/InteractsWithTwigComponentsTest.php | 3 +++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/TwigComponent/tests/Fixtures/Component/WithSlotsAndLimitedScope.php create mode 100644 src/TwigComponent/tests/Fixtures/templates/components/WithSlotsAndLimitedScope.html.twig diff --git a/src/TwigComponent/tests/Fixtures/Component/WithSlotsAndLimitedScope.php b/src/TwigComponent/tests/Fixtures/Component/WithSlotsAndLimitedScope.php new file mode 100644 index 00000000000..257d03fe468 --- /dev/null +++ b/src/TwigComponent/tests/Fixtures/Component/WithSlotsAndLimitedScope.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\UX\TwigComponent\Tests\Fixtures\Component; + +use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; + +/** + * @author Kevin Bond + */ +#[AsTwigComponent] +final class WithSlotsAndLimitedScope +{ +} diff --git a/src/TwigComponent/tests/Fixtures/templates/components/WithSlotsAndLimitedScope.html.twig b/src/TwigComponent/tests/Fixtures/templates/components/WithSlotsAndLimitedScope.html.twig new file mode 100644 index 00000000000..084980f815a --- /dev/null +++ b/src/TwigComponent/tests/Fixtures/templates/components/WithSlotsAndLimitedScope.html.twig @@ -0,0 +1,7 @@ +
+ {% with { outerScope } only %} + {% block content %}{% endblock %} + {% block slot1 %}{% endblock %} + {% block slot2 %}{% endblock %} + {% endwith %} +
diff --git a/src/TwigComponent/tests/Integration/ComponentFactoryTest.php b/src/TwigComponent/tests/Integration/ComponentFactoryTest.php index ab4639ad0de..0869782f112 100644 --- a/src/TwigComponent/tests/Integration/ComponentFactoryTest.php +++ b/src/TwigComponent/tests/Integration/ComponentFactoryTest.php @@ -261,7 +261,7 @@ public function testCannotGetConfigByNameForNonRegisteredComponent() * @testWith ["tabl", "Unknown component \"tabl\". Did you mean this: \"table\"?"] * ["Basic", "Unknown component \"Basic\". Did you mean this: \"BasicComponent\"?"] * ["basic", "Unknown component \"basic\". Did you mean this: \"BasicComponent\"?"] - * ["with", "Unknown component \"with\". Did you mean one of these: \"with_attributes\", \"with_exposed_variables\", \"WithSlots\"?"] + * ["with", "Unknown component \"with\". Did you mean one of these: \"with_attributes\", \"with_exposed_variables\", \"WithSlots\", \"WithSlotsAndLimitedScope\"?"] * ["anonAnon", "Unknown component \"anonAnon\". And no matching anonymous component template was found."] */ public function testCannotGetInvalidComponent(string $name, string $expectedExceptionMessage) diff --git a/src/TwigComponent/tests/Integration/Test/InteractsWithTwigComponentsTest.php b/src/TwigComponent/tests/Integration/Test/InteractsWithTwigComponentsTest.php index 1d2b78524c5..6c8b0620bdd 100644 --- a/src/TwigComponent/tests/Integration/Test/InteractsWithTwigComponentsTest.php +++ b/src/TwigComponent/tests/Integration/Test/InteractsWithTwigComponentsTest.php @@ -15,6 +15,7 @@ use Symfony\UX\TwigComponent\Test\InteractsWithTwigComponents; use Symfony\UX\TwigComponent\Tests\Fixtures\Component\ComponentA; use Symfony\UX\TwigComponent\Tests\Fixtures\Component\WithSlots; +use Symfony\UX\TwigComponent\Tests\Fixtures\Component\WithSlotsAndLimitedScope; use Symfony\UX\TwigComponent\Tests\Fixtures\Service\ServiceA; final class InteractsWithTwigComponentsTest extends KernelTestCase @@ -87,5 +88,7 @@ public static function withSlotsNameProvider(): iterable { yield ['WithSlots']; yield [WithSlots::class]; + yield ['WithSlotsAndLimitedScope']; + yield [WithSlotsAndLimitedScope::class]; } } From 3b4bc6e6ba8e86a17b352229ab6f48f42b47718c Mon Sep 17 00:00:00 2001 From: Jano Paetzold Date: Thu, 4 Dec 2025 18:09:12 +0100 Subject: [PATCH 2/2] Allow InteractsWithTwigComponents to render blocks when variable scope is limited as long as `outerScope` is included This is more cache-friendly than to inline the content of the block into the Twig template. But it requires Twig component authors to allow the `outerScope` variable in their blocks. --- src/TwigComponent/src/Test/InteractsWithTwigComponents.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TwigComponent/src/Test/InteractsWithTwigComponents.php b/src/TwigComponent/src/Test/InteractsWithTwigComponents.php index 6de97629cc0..f7a5be7ea29 100644 --- a/src/TwigComponent/src/Test/InteractsWithTwigComponents.php +++ b/src/TwigComponent/src/Test/InteractsWithTwigComponents.php @@ -52,7 +52,7 @@ protected function renderTwigComponent(string $name, array $data = [], ?string $ $template = \sprintf('{%% component "%s" with data %%}', addslashes($name)); foreach (array_keys($blocks) as $blockName) { - $template .= \sprintf('{%% block %1$s %%}{{ blocks.%1$s|raw }}{%% endblock %%}', $blockName); + $template .= \sprintf('{%% block %1$s %%}{{ outerScope.blocks.%1$s|raw }}{%% endblock %%}', $blockName); } $template .= '{% endcomponent %}';