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 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢