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

Conversation

squrious
Copy link
Contributor

@squrious squrious commented May 2, 2024

Q A
Bug fix? yes
New feature? no
Issues -
License MIT

It looks like #1652 introduced an issue with input props and parent context.

If:

  • A component prop has the same name as a variable in the parent context
  • The context variable is used to set the prop value as an input prop
  • The component is rendered with the embed strategy

Then, the default value of the prop is used instead of the one passed to the component.

Example:

{# templates/components/Hello.html.twig #}

{% props name = 'John' %}
<div {{ attributes }}>
    Hello {{ name }}!
</div>

Render embedded

{% set name = 'Bryan' %}
<twig:Hello :name="name"></twig:Hello>

Wrong output:

<div>
    Hello John!
</div>

Render with function

{% set name = 'Bryan' %}
<twig:Hello :name="name" />

Correct output:

<div>
    Hello Bryan!
</div>

@carsonbot carsonbot added Bug Bug Fix TwigComponent Status: Needs Review Needs to be reviewed labels May 2, 2024
@squrious squrious force-pushed the twig-component/input-prop-with-same-name-in-context branch from 036abf8 to 2d48001 Compare May 2, 2024 16:12
@@ -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));
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

['__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)],
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
['__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)

Copy link
Contributor Author

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 :)

@smnandre
Copy link
Member

smnandre commented May 2, 2024

Maybe add a test case when name is passed with "null" ?

@squrious
Copy link
Contributor Author

squrious commented May 3, 2024

Thanks for the review @smnandre!

Maybe add a test case when name is passed with "null" ?

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 isset in the PropsNode class, so I thought it was intended for some reason. Would you prefer to replace them with array_key_exists?

@squrious squrious force-pushed the twig-component/input-prop-with-same-name-in-context branch from 2d48001 to 7c4030b Compare May 3, 2024 07:18
@smnandre
Copy link
Member

smnandre commented May 3, 2024

Oh we lost some hair with @WebMamba last time, i will trust you on this ^^

Copy link
Contributor

@WebMamba WebMamba left a 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!

@carsonbot carsonbot added Status: Reviewed Has been reviewed by a maintainer and removed Status: Needs Review Needs to be reviewed labels May 4, 2024
@kbond
Copy link
Member

kbond commented May 8, 2024

This is great, thanks Nicolas!

@kbond kbond merged commit 57c014f into symfony:2.x May 8, 2024
27 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Status: Reviewed Has been reviewed by a maintainer TwigComponent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants