在树枝上展示无限的儿童类别

时间:2018-05-18 13:03:25

标签: php twig

我有这样的数组:

array(
  'id' => 1,
  'children' => array(
    'id' => 5,
    'children' =>
    array(
      'id' => 6,
      'children' => 'none',
    ),
    array(
      'id' => 8,
      'children' => array(...),
    )
  )
)
使用php代码可以调用Recursive Function来回显它。但是在树枝上我不能称之为递归函数。

{% for category in categories %}
    <div class="category">
      <div class="id">{{ category.id }}</div>
    </div>
{% endfor %}

我的代码只打印第一级数组,而不是其他!

1 个答案:

答案 0 :(得分:0)

你可以用积木:

{% set foo = { 'hello': { 'hello': { 'hello': 'swagg' }, 'foo': 'bar' } } %}


{% block test %}
<ul>
    {% for keyOfFoo, f in foo %}
    <li>
        {{ keyOfFoo }}
        {% if f is iterable %}
            {% with { foo: f } %}
            {{ block('test') }}
            {% endwith %}
        {% endif %}
    </li>
    {% endfor %}
</ul>
{% endblock test %}

这是现场演示:https://twigfiddle.com/r35pae

但是还有很多其他选择。像include循环一样。或embedded

相关问题