ux icon indicating copy to clipboard operation
ux copied to clipboard

Feature Request: Allow Access to Parent Context in Single Tag Component Syntax

Open FACHINA opened this issue 6 months ago • 2 comments

Hello Symfony UX Team,

First, thank you for the great work on the UX Twig Components, which bring a much-needed modern templating experience to Symfony!

I'd like to propose a feature request or open a discussion regarding component context behavior:

Current Behavior

When using the single tag syntax for components (e.g. <MyComponent />), the component does not have access to the parent template’s context. This requires all needed data to be explicitly passed as props:

<MyComponent someProp="{{ someParentVar }}" />

However, when using the block/paired syntax, the content inside the component has access to the parent scope:

<MyComponent>
    {{ someParentVar }} {# This works #}
</MyComponent>

Feature Request

Would it be possible to make the parent context available (even optionally) to components rendered via the single tag syntax?

This would greatly improve ergonomics for smaller or frequently used components where repeating someProp="{{ someParentVar }}" can be verbose, especially when many variables need to be reused.

Considerations

I understand that component isolation is an intentional design choice, and I'm not suggesting full scope leakage. But perhaps a middle ground could be:

  • An optional flag on the component class or template to enable access to parent context in single tag mode.
  • A config-level opt-in for looser isolation (e.g., for internal projects where strict encapsulation isn't required).

Use Case Example

{# in template #}
{% set color = 'blue' %}
<MyComponent />
{# inside MyComponent.html.twig #}
<div style="color: {{ color }};">Hello!</div>

Currently, this only works if you pass color explicitly or use block syntax.

FACHINA avatar Jun 05 '25 14:06 FACHINA

See my comment here, as the RFC and the othen one sum up the current situation perfectly: https://github.com/symfony/ux/issues/2818#issuecomment-2940918570

What you wish for here is very legit and I get the need, as I understand the DX improvment.

I also get why people would fear this very much, for performance, testability and security issues.

So let's wait a bit and we'll decide what and how we can do this. Currently it would be hard to implement properly.

The flag is not in itself a possibility, because the caller must decide what going down, not the opposite :|

And it would also break the Symfony BC promise, as much it would have a big impact on people using the self-closing components for this particular isolation :)

I'd suggest we talk about this topic in a couple of weeks.

smnandre avatar Jun 06 '25 03:06 smnandre

Thank you for this suggestion. There has not been a lot of activity here for a while. Would you still like to see this feature? Every feature is developed by the community. Perhaps someone would like to try? You can read how to contribute to get started.

carsonbot avatar Dec 07 '25 12:12 carsonbot

Could I get a reply or should I close this?

carsonbot avatar Dec 21 '25 12:12 carsonbot