使用 Twig 变量作为其他变量的一部分
Use a Twig variable as a part of an other
我想使用来自 "for" 循环的 Twig 值作为另一个 for 循环中的键。
我有 2 个数组:"tab" dans "stats"。
"tab" 数组有多个值,其中一个名为 "name",这是另一个包含键和值的数组。
我想将 "tab.name" 值用作我的统计数组的循环变量。
我尝试直接在我的变量中 "add" 值,但没有成功。
{% for elem in tab %}
{% for data in stats.elem.name %}
------ My code
{% endfor %}
{% endfor %}
有了这个我没有任何结果,因为 Twig 搜索 "stats.elem.name" 数组,但实际上例如如果在循环中 elem.name = "intitule",真正的数组是stats.intitule.
希望我说得够清楚了……:)
谢谢!
您可以通过将统计数据的数组引用更改为标准方括号来实现 - stats[elem.name]
{% for elem in tab %}
{% for data in stats[elem.name] %}
<!-- code -->
{% endfor %}
{% endfor %}
这应该有效
{% for elem in tab %}
{% for data in stats[elem] %}
{{data}}
{% endfor %}
{% endfor %}
我想使用来自 "for" 循环的 Twig 值作为另一个 for 循环中的键。
我有 2 个数组:"tab" dans "stats"。 "tab" 数组有多个值,其中一个名为 "name",这是另一个包含键和值的数组。 我想将 "tab.name" 值用作我的统计数组的循环变量。
我尝试直接在我的变量中 "add" 值,但没有成功。
{% for elem in tab %}
{% for data in stats.elem.name %}
------ My code
{% endfor %}
{% endfor %}
有了这个我没有任何结果,因为 Twig 搜索 "stats.elem.name" 数组,但实际上例如如果在循环中 elem.name = "intitule",真正的数组是stats.intitule.
希望我说得够清楚了……:)
谢谢!
您可以通过将统计数据的数组引用更改为标准方括号来实现 - stats[elem.name]
{% for elem in tab %}
{% for data in stats[elem.name] %}
<!-- code -->
{% endfor %}
{% endfor %}
这应该有效
{% for elem in tab %}
{% for data in stats[elem] %}
{{data}}
{% endfor %}
{% endfor %}