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

[TwigComponents] [Question] Render component using dynamically passed name #1922

Open
Pechynho opened this issue Jun 15, 2024 · 7 comments
Open
Labels
question Further information is requested TwigComponent

Comments

@Pechynho
Copy link

Hi,

I'm trying to render a Twig component using a dynamically passed name. Is there a way to achieve this in Twig? Here is an example of what I'm attempting to do:

{% set my_string = 'Modal' %}
{% component (my_string) with {type: 'success'} %}
    {% block content %}{{ ...content }}{% endblock %}
{% endcomponent %}

I want to be able to dynamically set the component name using the my_string variable. Any guidance or suggestions on how to accomplish this would be greatly appreciated!

Thank you!

@carsonbot carsonbot added the question Further information is requested label Jun 15, 2024
@WebMamba
Copy link
Collaborator

Do you have an error or something when executing this? You can also do this:

{% set my_string = 'Modal' %}

{{ component(my_string, {
   type: 'success',
   content: ...content
})

but here you need to have your content in a props.

@smnandre
Copy link
Collaborator

@WebMamba is right, the only way to call component dynamically is with the component function.

Currently there is no possibility to embed content into components with dynamic name.

@seb-jean
Copy link
Contributor

Maybe we could take inspiration from this: https://laravel.com/docs/11.x/blade#dynamic-components

@smnandre
Copy link
Collaborator

You can call dynamic component already. It's embedding a component that does not work

@seb-jean
Copy link
Contributor

Sorry! ☺️

@tibobaldwin
Copy link

Inspired by what @seb-jean said, we could perhaps create a dynamic-component component like for icons and then use it with HTML Syntax.

Without content:

{% set component_name = 'Modal' %}

<twig:ux:dynamic-component component="{{ component_name }}" />

With content:

{% set component_name = 'Modal' %}

<twig:ux:dynamic-component component="{{ component_name }}">
    {{ block(outerBlocks.content) }}
</twig:ux:dynamic-component>

So the content can be passed with dynamic component names.

@smnandre
Copy link
Collaborator

smnandre commented Jul 1, 2024

This is not the real problem, for now the component tag does not allow dynamic bloc names.

The HTML syntax is only sugar on top of it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested TwigComponent
Projects
None yet
Development

No branches or pull requests

6 participants