[TwigComponent] Support component tag names from expressions and component objects#3395
Open
andreybotanic wants to merge 3 commits intosymfony:2.xfrom
Open
[TwigComponent] Support component tag names from expressions and component objects#3395andreybotanic wants to merge 3 commits intosymfony:2.xfrom
andreybotanic wants to merge 3 commits intosymfony:2.xfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Currently, there are three ways to render a component:
{% component %}tagcomponent()functionEach approach has its own limitations:
component()function supports dynamic component names, but does not allow blocks.{% component %}tag supports blocks, but does not allow dynamic component names.{% component %}tag.Proposal
Add support for dynamic component names in the
{% component %}tag.This would enable a unified syntax that supports both:
The HTML-like syntax is intentionally left unchanged, as introducing dynamic names there would result in awkward and hard-to-read syntax.
Example Usage
{% set componentName = 'MyComponent' %} {% component componentName %} {% endcomponent %}Extended Idea
Allow passing a component instance directly, instead of its name:
{% component myComponent %} {% endcomponent %}Where
myComponentis an object of a class marked with:or
Real-world Use Case
This proposal was inspired by a real scenario in my project:
{% for tab in this.tabs %} {% component tab with {loading: 'lazy'} %} {% block loadingContent %} <div class="d-flex align-items-center w-75 mx-auto"> <strong role="status">Loading...</strong> <div class="spinner-border ms-auto" aria-hidden="true"></div> </div> {% endblock %} {% endcomponent %} {% endfor %}In this case,
tabis dynamic, and there is a need to:loading)loadingContent)Currently, this is not achievable with existing syntax without compromises.
Benefits
Notes