Possible feature proposal: Dynamic Twig Component #3669
Closed
Pechynho
started this conversation in
RFC - Feature Ideas
Replies: 2 comments
|
I would be in favor of a syntax similar to Vue (https://vuejs.org/guide/essentials/component-basics.html#dynamic-components): <twig:Component is="MyComponentName">
...
</twig:Component>I'm not sure why do you need a Note that #3395 could be related |
0 replies
|
Yeah, #3395 looks like much better approach to this problem. Gonna keep eye on that :) |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Possible feature proposal: Dynamic Twig Component
Hi,
I had a use case where I needed to render Twig Components dynamically, including support for content and named blocks.
For example:
which would be equivalent to:
I ended up implementing this myself and I'm wondering whether this is something the project might be interested in supporting natively.
Current implementation:
ComponentRendererInterface
ComponentRenderer
DynamicComponent
DynamicComponent template
{%- set content = block('content') is defined ? block('content') : null -%} {%- set blocksMap = {} -%} {%- for blockName in blocks -%} {%- if block(blockName) is defined -%} {%- set blocksMap = blocksMap|merge({(blockName): block(blockName)}) -%} {%- endif -%} {%- endfor -%} {{- this.render(attributes.all, content, blocksMap)|raw -}}Usage
The implementation also supports named blocks:
Internally, the renderer generates a Twig component invocation, caches it in the cache directory and renders it through the existing Twig Component infrastructure.
Before spending time polishing this and preparing a PR, I'd like to know whether maintainers see value in supporting dynamic Twig Components with content and named blocks, and whether a contribution in this area would be welcome.
All reactions