2

How do I add dividers between each <li> in the menu.html.twig template. Obviously with the code below, the divider also gets added after the last item, which isn't the desired result.

{% import _self as menus %}

{#
  We call a macro which calls itself to render the full tree.
  @see http://twig.sensiolabs.org/doc/tags/macro.html
#}
{{ menus.menu_links(items, attributes, 0) }}

{% macro menu_links(items, attributes, menu_level) %}
  {% import _self as menus %}
  {% if items %}
    <ul class="loginbar pull-right">
    {% for item in items %}
      {%
        set classes = [
          item.is_expanded ? 'menu-item--expanded',
          item.is_collapsed ? 'menu-item--collapsed',
          item.in_active_trail ? 'menu-item--active-trail',
        ]
      %}
      <li{{ item.attributes.addClass(classes) }}>
        {{ link(item.title, item.url) }}
        {% if item.below %}
          {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
        {% endif %}
      </li>
      <li class="topbar-devider"></li>
    {% endfor %}
    </ul>
  {% endif %}
{% endmacro %}
2

See The Loop Variable in the Twig docs; you can use something like

{% if loop.last == false %}
  <li class="topbar-devider"></li>
{% endif %}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.