Skip to content
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] Parent function does not work in the content of a twig component #1875

Open
Nek- opened this issue May 29, 2024 · 6 comments · May be fixed by #1899
Open

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

Nek- opened this issue May 29, 2024 · 6 comments · May be fixed by #1899

Comments

@Nek-
Copy link
Contributor

Nek- commented May 29, 2024

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
Copy link
Collaborator

smnandre commented Jun 4, 2024

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 "twig:Select" component do ?

@Nek-
Copy link
Contributor Author

Nek- commented Jun 4, 2024

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
Copy link
Collaborator

smnandre commented Jun 6, 2024

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

@Nek-
Copy link
Contributor Author

Nek- commented Jun 7, 2024

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
Copy link
Collaborator

smnandre commented Jun 7, 2024

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
Copy link
Collaborator

WebMamba commented Jun 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants