user10264314
user10264314

Reputation:

PHP for-loop in 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>

Upvotes: 0

Views: 235

Answers (3)

K. B.
K. B.

Reputation: 1430

In twig very important close properly if statement and for statement... I have not checked this code it is working or no, but you can... :)

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

          <a href="{{ category.href }}" class="see-all">{{ text_all }} {{ category.name }}</a>

        </ul>
       </li>
      {% else %}
      <li><a href="{{ category.href }}">{{ category.name }}</a></li>
    {% endif %}
    </ul>
{% endfor %}
{% endif %}

Upvotes: 0

MoxGeek
MoxGeek

Reputation: 458

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

Upvotes: 1

Emanuel Oster
Emanuel Oster

Reputation: 1306

Your last endfor-tag should be after the </ul>.

Upvotes: 0

Related Questions