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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里