【问题标题】:show unlimited children category on twig在树枝上显示无限儿童类别
【发布时间】:2018-05-18 13:03:25
【问题描述】:

我有这样的数组:

array(
  'id' => 1,
  'children' => array(
    'id' => 5,
    'children' =>
    array(
      'id' => 6,
      'children' => 'none',
    ),
    array(
      'id' => 8,
      'children' => array(...),
    )
  )
)

用php代码可以调用递归函数来回显它。但在树枝中我不能调用递归函数。

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

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

【问题讨论】:

标签: php twig


【解决方案1】:

你可以用积木:

{% 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多