Twig 动态获取以数字结尾的对象名称

Twig get object names ending with numbers dynamically

我有一些对象,例如 address1、address2、address3 ... address10。所有这些对象都有 latlong 值。

我知道我们可以从 Twig 的 attribute() 函数中获取它,但我想要在我的 twig 模板中获取主要对象

 {% for i in 1..10 %}
      {% set address = address~i %}
      {{ address.lat }}
      // or like
      {{ attribute(address, 'lat') }}
 {% endfor %}

只需简单地使用:

 {% for i in 1..10 %}
      {{ attribute(attribute(_context, 'address'~i), 'lat') }}
 {% endfor %}