Skip to content

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

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

Open
FACHINA opened this issue Jun 5, 2025 · 1 comment
Labels

Comments

@FACHINA
Copy link

FACHINA commented Jun 5, 2025

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 FACHINA added the RFC label Jun 5, 2025
@smnandre
Copy link
Member

smnandre commented Jun 6, 2025

See my comment here, as the RFC and the othen one sum up the current situation perfectly: #2818 (comment)

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.

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

No branches or pull requests

2 participants