diff --git a/src/TwigComponent/src/ComponentAttributes.php b/src/TwigComponent/src/ComponentAttributes.php index 751e324306e..574a385d69e 100644 --- a/src/TwigComponent/src/ComponentAttributes.php +++ b/src/TwigComponent/src/ComponentAttributes.php @@ -149,8 +149,12 @@ public function add($stimulusDto): self return $clone->defaults($controllersAttributes); } - public function remove($key): void + public function remove($key): self { - unset($this->attributes[$key]); + $attributes = $this->attributes; + + unset($attributes[$key]); + + return new self($attributes); } } diff --git a/src/TwigComponent/src/Twig/PropsNode.php b/src/TwigComponent/src/Twig/PropsNode.php index 17aebc43ef7..250b0ea2be1 100644 --- a/src/TwigComponent/src/Twig/PropsNode.php +++ b/src/TwigComponent/src/Twig/PropsNode.php @@ -31,7 +31,7 @@ public function compile(Compiler $compiler): void foreach ($this->getAttribute('names') as $name) { $compiler ->addDebugInfo($this) - ->write('$context[\'attributes\']->remove(\''.$name.'\');') + ->write('$context[\'attributes\'] = $context[\'attributes\']->remove(\''.$name.'\');') ->write('if (!isset($context[\''.$name.'\'])) {') ; diff --git a/src/TwigComponent/tests/Fixtures/templates/anonymous_component_none_scalar_prop.html.twig b/src/TwigComponent/tests/Fixtures/templates/anonymous_component_none_scalar_prop.html.twig index a1c267e1f1c..4bbccfb3fb7 100644 --- a/src/TwigComponent/tests/Fixtures/templates/anonymous_component_none_scalar_prop.html.twig +++ b/src/TwigComponent/tests/Fixtures/templates/anonymous_component_none_scalar_prop.html.twig @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/src/TwigComponent/tests/Fixtures/templates/components/UserCard.html.twig b/src/TwigComponent/tests/Fixtures/templates/components/UserCard.html.twig index 5242c2740ad..1a606024828 100644 --- a/src/TwigComponent/tests/Fixtures/templates/components/UserCard.html.twig +++ b/src/TwigComponent/tests/Fixtures/templates/components/UserCard.html.twig @@ -1,6 +1,6 @@ {% props user %} -
+

{{ user.name }}

{{ user.email }}

\ No newline at end of file diff --git a/src/TwigComponent/tests/Integration/ComponentExtensionTest.php b/src/TwigComponent/tests/Integration/ComponentExtensionTest.php index 8d6dbabf455..56c8f12db9e 100644 --- a/src/TwigComponent/tests/Integration/ComponentExtensionTest.php +++ b/src/TwigComponent/tests/Integration/ComponentExtensionTest.php @@ -189,6 +189,7 @@ public function testRenderAnonymousComponentWithNonScalarProps(): void $output = self::getContainer()->get(Environment::class)->render('anonymous_component_none_scalar_prop.html.twig', ['user' => $user]); + $this->assertStringContainsString('class="foo"', $output); $this->assertStringContainsString('Fabien', $output); $this->assertStringContainsString('test@test.com', $output); }