Skip to content

Commit 036abf8

Browse files
committed
[TwigComponent] Allow input props to have the same name as context variables
1 parent 3962582 commit 036abf8

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

src/TwigComponent/src/ComponentRenderer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,14 @@ private function preRender(MountedComponent $mounted, array $context = []): PreR
113113

114114
// expose public properties and properties marked with ExposeInTemplate attribute
115115
$props = array_merge($mounted->getInputProps(), $classProps);
116+
$propsKeys = \array_keys($props);
116117
$variables = array_merge(
117118
// first so values can be overridden
118119
$context,
119120
// add the context in a separate variable to keep track
120-
// of what is coming from outside the component
121-
['__context' => $context],
121+
// of what is coming from outside the component, excluding props
122+
// as they override initial context values
123+
['__context' => \array_filter($context, static fn($key) => !\in_array($key, $propsKeys), \ARRAY_FILTER_USE_KEY)],
122124
// keep reference to old context
123125
['outerScope' => $context],
124126
// add the component as "this"

src/TwigComponent/src/Twig/PropsNode.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,13 @@ public function compile(Compiler $compiler): void
100100
$compiler
101101
->write('if (isset($context[\'__context\'][\''.$name.'\'])) {')
102102
->raw("\n")
103-
->write('$contextValue = $context[\'__context\'][\''.$name.'\'];')
104-
->raw("\n")
105-
->write('$propsValue = $context[\''.$name.'\'];')
106-
->raw("\n")
107-
->write('if ($contextValue === $propsValue) {')
108-
->raw("\n")
103+
->indent()
109104
->write('$context[\''.$name.'\'] = ')
110105
->subcompile($this->getNode($name))
111106
->raw(";\n")
107+
->outdent()
112108
->write('}')
113109
->raw("\n")
114-
->write('}')
115110
;
116111
}
117112
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% set message = 'bar' %}
2+
3+
<twig:Message :message="message">
4+
<p>Hey!</p>
5+
</twig:Message>

src/TwigComponent/tests/Integration/ComponentExtensionTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ public function testComponentPropsOverwriteContextValue(): void
204204
$this->assertStringContainsString('<p>foo</p>', $output);
205205
}
206206

207+
public function testComponentPropsOverwriteContextValueWithInputProp(): void
208+
{
209+
$output = self::getContainer()->get(Environment::class)->render('anonymous_component_with_input_prop_with_same_name_in_context.html.twig');
210+
211+
$this->assertStringContainsString('<p>bar</p>', $output);
212+
}
213+
207214
public function testComponentPropsWithTrailingComma(): void
208215
{
209216
$output = self::getContainer()->get(Environment::class)->render('anonymous_component_props_trailing_comma.html.twig');

0 commit comments

Comments
 (0)