Skip to content

[TwigComponent] Allow input props to have the same name as context variables #1820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
7 changes: 4 additions & 3 deletions src/TwigComponent/src/ComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ private function preRender(MountedComponent $mounted, array $context = []): PreR
// first so values can be overridden
$context,
// add the context in a separate variable to keep track
// of what is coming from outside the component
['__context' => $context],
// of what is coming from outside the component, excluding props
// as they override initial context values
['__context' => array_diff_key($context, $props)],
// keep reference to old context
['outerScope' => $context],
// add the component as "this"
Expand Down Expand Up @@ -180,7 +181,7 @@ private function exposedVariables(object $component, bool $exposePublicProps): \
$name = $attribute->name ?? (str_starts_with($method->name, 'get') ? lcfirst(substr($method->name, 3)) : $method->name);

if ($method->getNumberOfRequiredParameters()) {
throw new \LogicException(sprintf('Cannot use %s on methods with required parameters (%s::%s).', ExposeInTemplate::class, $component::class, $method->name));
throw new \LogicException(sprintf('Cannot use "%s" on methods with required parameters (%s::%s).', ExposeInTemplate::class, $component::class, $method->name));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good fabbot

}

if ($attribute->destruct) {
Expand Down
11 changes: 3 additions & 8 deletions src/TwigComponent/src/Twig/PropsNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function compile(Compiler $compiler): void
if (!$this->hasNode($name)) {
$compiler
->indent()
->write('throw new \Twig\Error\RuntimeError("'.$name.' should be defined for component '.$this->getTemplateName().'");')
->write('throw new \Twig\Error\RuntimeError("'.$name.' should be defined for component '.$this->getTemplateName().'.");')
->write("\n")
->outdent()
->write('}')
Expand Down Expand Up @@ -100,18 +100,13 @@ public function compile(Compiler $compiler): void
$compiler
->write('if (isset($context[\'__context\'][\''.$name.'\'])) {')
->raw("\n")
->write('$contextValue = $context[\'__context\'][\''.$name.'\'];')
->raw("\n")
->write('$propsValue = $context[\''.$name.'\'];')
->raw("\n")
->write('if ($contextValue === $propsValue) {')
->raw("\n")
->indent()
->write('$context[\''.$name.'\'] = ')
->subcompile($this->getNode($name))
->raw(";\n")
->outdent()
->write('}')
->raw("\n")
->write('}')
;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% set message = 'bar' %}

<twig:Message :message="message">
<p>Hey!</p>
</twig:Message>
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ public function testComponentPropsOverwriteContextValue(): void
$this->assertStringContainsString('<p>foo</p>', $output);
}

public function testComponentPropsOverwriteContextValueWithInputProp(): void
{
$output = self::getContainer()->get(Environment::class)->render('anonymous_component_with_input_prop_with_same_name_in_context.html.twig');

$this->assertStringContainsString('<p>bar</p>', $output);
}

public function testComponentPropsWithTrailingComma(): void
{
$output = self::getContainer()->get(Environment::class)->render('anonymous_component_props_trailing_comma.html.twig');
Expand Down