symfony / ux

Symfony UX initiative: a JavaScript ecosystem for Symfony
https://ux.symfony.com/
MIT License
860 stars 315 forks source link

[TwigComponent] Parent function does not work in the content of a twig component #1875

Open Nek- opened 6 months ago

Nek- commented 6 months ago

I tried the following stupid code:

{% block choice_row %}
    <twig:Select label="{{ form.vars.label|default(name|humanize)|trans }}">
        {{ parent() }}
    </twig:Select>
{% endblock %}

But the parent() function does not work inside a twig component. 😅 It was a bit disturbing. Not sure it's a bug or a feature request to be honest.

smnandre commented 6 months ago

Hi @Nek- !

Not sure why it would be stupid :) .... but it won't work indeed.

Twig Components are isolated component, somehow like includes or embed in Twig.

So the parent() you're trying to call is not in the "choice_row" scope, but in the twig:Select one :)

What you tried is similar to doing something like this :

{# Your template #}

{% block choice_row %}
    {{ include('foo.html.twig') }}
{% endblock %}
{# foo.html.twig #} 

{{ parent() }}

That would not work, as foo.html.twig has no parent.


Just beeing curious, what does your "" component do ?

Nek- commented 6 months ago

In my point of view, it's far from obvious that the scope change in the code I give an argument to the component. (Maybe I'm used to ReactJS so it does not feel fluid for me). by stupid code I meant very simple that actually does not work

My twig:Select is a custom component that renders a fancy select that makes use of JS for some features. It embeds a standard <select> and updates its values.

I wanted to use it like this because I'm using the component in other contexts that Symfony forms.

smnandre commented 5 months ago

Ok! So let's try another way to explain :) How would you do the same thing with an embed tag ? :)

Nek- commented 5 months ago

Do you mean this kind of things? (I never used it!)

I think by design embedding will not work with such feature. But it's also because it provides different features, like extending the include. Does the twig component element provide any specific feature that conflicts?

smnandre commented 5 months ago

As i tried to explain, TwigComponent are not only twig syntax, they enforce isolation.

What i then tried to tell you is that it's normal it would not work, as it would not work either with embed, and TwigComponent with content are some kind of embeds :)

But i know i'm not always the best to explain my ideas 😅

WebMamba commented 5 months ago

Hey @Nek- I just created a PR tell me if you think that can be useful 😁 Thanks your report