1

I'm working on creating components in twig, and since this is supposed to be a "somewhat replacement" for front end components (like ember/react), I wanted to see if there is a way to replicate some functionality.

For instance, I'm working on creating a tab list. Within that tab list there is the associated panel being shown for its content. Here is the twig component:

{% set tabID = "tabs_" ~ random() %}
<div>
    <div class="sm:hidden">
    <label for="tabs" class="sr-only">
        Select a tab
    </label>
    <select
        id="{{tabID}}"
        name="{{tabID}}}}"
        value={% for tab in tabs|filter(a => a.current == true) %} {{tab.name}}{% endfor %}
        class="block w-full rounded-md border-gray-300 py-2 pl-3 pr-10 text-base focus:border-indigo-500 focus:outline-none focus:ring-indigo-500 sm:text-sm"
    >
        {% for tab in tabs %}
            <option {% if tab.current %} selected {% endif %} key={{tab.name}}>{{tab.name}}</option>
        {% endfor %}
    </select>
    </div>
    <div class="hidden sm:block">
        <div class="border-b border-gray-200">
            <nav aria-label="Tabs" class="-mb-px flex space-x-8" role="tablist">
            {% for tab in tabs %}
                <button
                aria-selected='{{ tab.current ? "true" : "false" }}'
                aria-controls='{{tab.name}}'
                role='tab'
                class='whitespace-nowrap border-b-2 px-1 py-4 text-sm font-medium {{
                    tab.current ? "border-indigo-500 text-indigo-600" : "border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700"
                }}'
                >
                {{tab.name}}
                </button>
            {% endfor %}
            </nav>
        </div>
    </div>
    <div class='panel-content'>
        {% for tab in tabs %}
            {% set nameBlock = 'panel_' ~ loop.index %}
            <div class="panel {% if not tab.current %} hidden {% endif %}">
                {% block nameBlock %}
                        
                    
                {% endblock %}
            </div>
        {% endfor %}
    </div>
</div>

As you can see, I'm attempting to create a dynamic block (which isn't supported, from what I can tell) so that this can be possible:

<twig:Tabs tabs="{{tabs}}">
            <twig:block name="panel_0">
                GROSS 1
            </twig:block>
            <twig:block name="panel_1">
                GROSS 2 
            </twig:block>
            <twig:block name="panel_2">
                GROSS 3 
            </twig:block>
            <twig:block name="panel_3">
                GROSS 4
            </twig:block>
    </twig:Tabs>

How do I achieve something like this? Would I have to create this using the php component file and utilize the #[PostMount] function? Or is the only way to do this is to manually build a sub component called panel that gets used?

10
  • Can you elaborate why you need the dynamic blocks? You could always have a look at {% embed %} Commented Nov 22, 2024 at 17:48
  • It's so that in this scenario the panel section is auto generated by the number of tabs you inputted. Commented Nov 22, 2024 at 18:26
  • But why would you need the block? What is the purpose of the blocks you're trying to create? What are you gonna do with the blocks? Commented Nov 22, 2024 at 19:47
  • 1
    I've edited the question to point out your are actually using Symfony's UX Twig component. From what I can tell by reading through the docs, I'd assume you would want to create separate component, e.g. Tab which has the block-defintion, which you already pointed out in your question as well: "the only way to do this is to manually build a sub component". I also came across this: Welcome to Symfony UX Discussions! which seems still active. If I were you I would post the same question over there, or create a question there and link to this thread. Commented Nov 23, 2024 at 8:53
  • 1
    [...] Chances are you'll get a faster response over there, than here (I haven't seen much TwigComponent questions here and I don't have a Symfony workspace set-up atm) Commented Nov 23, 2024 at 8:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.