Symfony 将字符串连接为细枝中的变量

Symfony 将字符串连接为细枝中的变量,symfony,twig,Symfony,Twig,我有一个表单可以为每个类别生成字段,所以如果我有5个类别,twig将生成5个类别字段 此数组由5个类别组成,因此将循环5次 {% set categories = ['attraction', 'featured_artist', 'co_presentator', 'major_sponsor', 'minor_sponsor'] %} 我将计算总类别,并将其减去1以使用for循环。我们在数组中使用,所以它将类似于0,1,2,3。。等等 {% set total_category = cat

我有一个表单可以为每个类别生成字段,所以如果我有5个类别,twig将生成5个类别字段

此数组由5个类别组成,因此将循环5次

{% set categories = ['attraction', 'featured_artist', 'co_presentator', 'major_sponsor', 'minor_sponsor'] %}
我将计算总类别,并将其减去1以使用for循环。我们在数组中使用,所以它将类似于0,1,2,3。。等等

{% set total_category = categories|count -1 %}
现在循环从这里开始,我在数据库中有一个不同的列,叫做吸引力图片,主要赞助商图片,特色艺术家图片。因此,我必须计算每个类别在该字段中的总值

{% for i in 0..total_category %}

    <div class="increment-field-container" data-increment-host="{{ categories[i] }}">
        {% set total_field = details.attraction_image|explode|count %}

    </div>
{% endfor %}
但我想发生的是

{% set total_field = details.categories[i]_image|explode|count %}
从技术上讲,这是不正确的:

我尝试了这个字符串插值的方法,但仍然不起作用

{% set total_field = details."#{categories[i]}"_image|explode|count %}

这个问题有什么解决方案吗?

您可以使用。有关详细信息,请参见我的。

在等待回答时,我正在阅读该文档,对此感到非常困惑。现在,您澄清了如何在其他问题中使用它。非常感谢!:)
{% set total_field = details."#{categories[i]}"_image|explode|count %}