Search code examples
twig

Dynamic block name in TWIG


I need to add multiple blocks in my template, every with different name.

{% for item from items %}
    {% block item.name %}sometext{% endblock %}
{% endfor %}

But I get error. How can I do this ?

In


Solution

  • You can load blocks dynamically using the block function.

    {% for item in items %}
        {{ block( item.name )|raw }}
    {% endfor %}
    

    Twig documentation for the block function