在树枝中循环嵌套数组 [英] Loop nested array in twig
本文介绍了在树枝中循环嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用 Twig 遍历这个(对我来说)相当复杂的数组.我希望能够打印出所有元素.你会怎么做?下面是数组和我目前正在尝试的代码.
Im trying to loop through this (for me) quite complex array with Twig. I want to be able to print out all elements. How would you do that? Below is the array and the code im currently trying.
这是数组:https://pastebin.com/TZSANFpW
到目前为止我已经尝试过这个,但它给了我错误:注意:数组到字符串的转换"
I have tried this so far but it gives me error: "Notice: Array to string conversion in "
{% for route in routes %}
<p>
{{ route.admin }}
</p>
{% endfor %}
推荐答案
为了读出一个完整的数组,您需要创建某种形式的递归.您可以使用
In order to read out a full array, you would to create some form of recursion. You could achieve this with a macro in the line of
macros.twig
{% macro readArray(array) %}
{% import _self as macros %}
{% if not array is iterable %}
{{ array }}
{% else %}
{% for k,v in array %}
<ul>
<li>
{{ k }}: {{ macros.readArray(v) }}
</li>
</ul>
{% endfor %}
{% endif %}
{% endmacro %}
main.twig
{% import "macros.twig" as macros %}
{{ macros.readArray(results) }}
这篇关于在树枝中循环嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文