Go to page content

Showing Menu Trail only for submenu items

It is sometimes useful to suppress the menu trail on top level items. Here is how to do it.


The menu_trail includes the whole path through the menu to the current page if it is reachable that way.  Sometimes it may seem pointless to show the menu_trail if we are on the first level of the menu.  If we want to avoid this we need to avoid rendering a menu trail when it is less than two items long.


Readers are assumed to be comfortable with Zotonic template design and the use of filters and tags.


One simple condition change to _article_chapeau.tpl from the blog skeleton makes this work:

{% with id|menu_trail as parents %}
    {% if parents|length > 1 %}
    <h5 class="chapeau">
    {% for p in parents %}
    <a href="{{ m.rsc[p].page_url }}">{{ m.rsc[p].title }}</a>
    {% if not forloop.last %}&raquo;{% endif %}
{% endfor %}</h5>{% endif %}{% endwith %}

The key here is {% if parents|length > 1 %} in place of {% if parents %}.

The if tag is now rendering the menu_trail only if there are two or more items in it which - as I mentioned before - happens when you are at least two levels deep in the menu.


There are no troubleshooting steps available for this guide.  Please provide any you have learned in the comments below or on the Zotonic Users Group.

This page is part of the Zotonic documentation, which is licensed under the Apache License 2.0.