Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic block name in TWIG

Tags:

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

like image 540
NagRock Avatar asked Feb 03 '23 15:02

NagRock


2 Answers

Dynamic block names are not possible with Twig. There has been a discussion about it over at GitHub.

like image 64
kgilden Avatar answered Feb 13 '23 23:02

kgilden


You can load blocks dynamically using the block function.

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

Twig documentation for the block function

like image 20
Carlos Llongo Avatar answered Feb 13 '23 23:02

Carlos Llongo