如何在循环中连接两个 Twig 变量
How to concatenate two Twig variables in a loop
我正在尝试创建动态变量名称,因此结果如下所示
{{ form_label(form.user_1) }}
{{ form_label(form.user_2) }}
{{ form_label(form.user_3) }}
{{ form_label(form.user_4) }}
这是我到目前为止尝试过的
{% for user in users %}
{{ form_label(form.user~'_'~loop.index) }}
{% endfor %}
但得到
Argument 1 passed to
Symfony\Component\Form\FormRenderer::searchAndRenderBlock() must be an
instance of Symfony\Component\Form\FormView, string given
我做错了什么?
我认为you have to use the attribute() function。
如果这不适用于 方法 参数中的串联,请尝试先在这样的变量中串联它:
{% set userIndex = 'user_' ~ loop.index %}
然后你应该试试这个:
{{ form_label(attribute(form, userIndex)) }}
我正在尝试创建动态变量名称,因此结果如下所示
{{ form_label(form.user_1) }}
{{ form_label(form.user_2) }}
{{ form_label(form.user_3) }}
{{ form_label(form.user_4) }}
这是我到目前为止尝试过的
{% for user in users %}
{{ form_label(form.user~'_'~loop.index) }}
{% endfor %}
但得到
Argument 1 passed to Symfony\Component\Form\FormRenderer::searchAndRenderBlock() must be an instance of Symfony\Component\Form\FormView, string given
我做错了什么?
我认为you have to use the attribute() function。
如果这不适用于 方法 参数中的串联,请尝试先在这样的变量中串联它:
{% set userIndex = 'user_' ~ loop.index %}
然后你应该试试这个:
{{ form_label(attribute(form, userIndex)) }}