开发者

Dynamic block name in TWIG

开发者 https://www.devze.com 2023-03-27 23:59 出处:网络
I need to add multiple blocks in my template, every with dif开发者_C百科ferent name. {% for item from items %}

I need to add multiple blocks in my template, every with dif开发者_C百科ferent name.

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

But I get error. How can I do this ?

In


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


You can load blocks dynamically using the block function.

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

Twig documentation for the block function


If I understood the question correctly, you can do this (use parent context):

parent.html.twig

{% for item from items %} 
  {% set currentLoopItemName = item.name %}
  {% block item_loop %}sometext{% endblock %}
{% endfor %}

override.html.twig

{% extends "base.html" %}

{% block item_loop %} 
  {% if item.name == 'custom' %}
      // do something
  {% else %}
     {{ parent() }}
  {% endif %}
{% endblock %}


I was attempting to do the same thing and found a solution using the template_from_string function.

_items.html.twig

{% for item in items %}
 {{ '{% block ' ~ item.name ~ ' %}'}}
 sometext
 {{ '{% endblock %}' }}
{% endfor %}
enter code here

page.html.twig

{% embed template_from_string(include('_items.html.twig')) %}
 {% block someItemName %} someDifferentText {% endblock %}
{% endembed %}

What's happening is the block tags are initially being created as text. Then we use the include function to get the rendered content of _items, as a string. Finally, we convert that string to a working template (which we can embed or extend).

This works because the template_from_string function will create and compile a template at runtime, where as normally twig is compiled before hand and unchanged at runtime.

0

精彩评论

暂无评论...
验证码 换一张
取 消