-
-
Notifications
You must be signed in to change notification settings - Fork 356
[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
[TwigComponent] Allow input props to have the same name as context variables #1820
Conversation
036abf8
to
2d48001
Compare
@@ -180,7 +182,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)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good fabbot
['__context' => $context], | ||
// of what is coming from outside the component, excluding props | ||
// as they override initial context values | ||
['__context' => array_filter($context, static fn ($key) => !\in_array($key, $propsKeys), \ARRAY_FILTER_USE_KEY)], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
['__context' => array_filter($context, static fn ($key) => !\in_array($key, $propsKeys), \ARRAY_FILTER_USE_KEY)], | |
['__context' => array_diff_key($context, $props)], |
(I have a doubt)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the tip! Fixed :)
Maybe add a test case when name is passed with "null" ? |
Thanks for the review @smnandre!
I wasn't sure about that. If you pass null, the prop is considered unset, so yes we can't pass a null value explicitly. But there is a systematic use of |
2d48001
to
7c4030b
Compare
Oh we lost some hair with @WebMamba last time, i will trust you on this ^^ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot @squrious, I over thinking this part, it's much simpler and less buggy! Great job!
This is great, thanks Nicolas! |
It looks like #1652 introduced an issue with input props and parent context.
If:
Then, the default value of the prop is used instead of the one passed to the component.
Example:
Render embedded
Wrong output:
Render with function
Correct output: