duanlu4371 2018-11-22 14:02
浏览 87
已采纳

PHP for循环在Twig中

I have a small problem with looping in Twig. How to loop the whole sub-nav? Currently it makes sub-nav in sub-nav nested.

{% if categories %}
    {% for category in categories %}
    <ul class="sub-nav">
      <li>
        <a href="{{ category.href }}" class="cat-name">{{ category.name }}</a>

        <ul class="sub-sub-nav list-inline">
          {% if category.children %}
          <li class="list-inline">
            <a class="sub-cat-name" href="">{% for children in category.children|batch(category.children|length / category.column|round(1, 'ceil')) %}</a>
            <ul class="sub-cat-menu">
              {% for child in children %}
              <li><a href="{{ child.href }}">{{ child.name }}</a></li>
              {% endfor %}
            </ul>
            {% endfor %}</li>
          <a href="{{ category.href }}" class="see-all">{{ text_all }} {{ category.name }}</a>
        </ul>
        {% endif %}
      </li>
      {% else %}
      <li><a href="{{ category.href }}">{{ category.name }}</a></li>
    {% endfor %}
    </ul>
  • 写回答

3条回答 默认 最新

  • doudu3961 2018-11-23 09:42
    关注

    after reading your dump , all it's ok i think you sould edit your code as below :

        {% if categories %}
        {% for category in categories %}
        <ul class="sub-nav">
          <li>
            <a href="{{ category.href }}" class="cat-name">{{ category.name }}</a>
    
            <ul class="sub-sub-nav list-inline">
              {% if category.children %}
              <li class="list-inline">
                <a class="sub-cat-name" href="">{% for children in category.children|batch(category.children|length / category.column|round(1, 'ceil')) %}</a>
                <ul class="sub-cat-menu">
                  {% for child in children %}
                  <li><a href="{{ child.href }}">{{ child.name }}</a></li>
                  {% endfor %}
                </ul>
                {% endfor %}</li>
              <a href="{{ category.href }}" class="see-all">{{ text_all }} {{ category.name }}</a>
            </ul>
            {% endif %}
          </li>
          {% else %}
          <li><a href="{{ category.href }}">{{ category.name }}</a></li>
    <!-- replace the end for  after the ul -->
        </ul>
    
      {% endfor %}
    

    feel free to comment here , so i can help you

    if it is not working , try to inspect your web page and search for random name from your db , what i did is search on the html output the word " scanner " etc that you have in your db . :) good lock

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?