Search code examples
templatesmenuitemwagtailseparator

Wagtail basic menu separators for all but last item in template


I am creating a basic bootstrap navbar menu in Wagtail based on steps 1 to 3 of this guide. My menu items need to have a separator in between each item like this :

Item One / Item Two / Item Three

However if I add a separator in I end up with one after the last item in the menu :

{% for menu in menuitems %}
    <a class="nav-item nav-link" href="{{menu.url}}">{{menu.title}}</a> /
{% endfor %}

Results in this ...

Item One / Item Two / Item Three /

How do I separate the items without adding a trailing divider?


Solution

  • Just change this:

    <a class="nav-item nav-link" href="{{menu.url}}">{{menu.title}}</a> /
    

    to this:

    <a class="nav-item nav-link" href="{{menu.url}}">{{menu.title}}</a>{% if not forloop.last %} /{% endif %}