Building and using site menus and navigation

Glass is ambivalent about your navigation markup and how your navigation relates to your site’s page structure. Practically, this means that menus need created manually, separate from creating pages, and that you need to handle them in your templates.

First build your navigation with the admin tools. We’ll assume that you made a tree called “Navigation”

Get the tree in a template

Anywhere in your template you can use the following tag to start building a navigation view.

{% menu “Navigation” %}

This will bring the top-level navigation into your template with no other context with the name “tree”. You can then display all of the top-level items with the below snippet:

<nav><ul>
 {% for item in tree.children %}
 <li><a href="{{ item.url }}">{{ item.name }}</a></li>
 {% endfor %}
</ul></nav>

If you want to recurse further into the structure, you can use nested loops:

<nav><ul>
 {% for item in tree.children %}
 <li><a href="{{ item.url }}">{{ item.name }}</a>
 {% for child in item.children %}
     {% if forloop.first %}<ul>{% endif %}
         <li><a href="{{ child.url }}">{{ child.name }}</a>
     {% if forloop.last %}</ul>{% endif %}
 {% endfor %}
 </li>
 {% endfor %}
</ul></nav>

There are several other options on the {% menu %} tag that provides context about the current page, and the breadcrumb.

{% menu “primary_navigation” %} {% menu “footer_nav” context_var=”bottom_tree” %} {% menu name

[begin_at=[“top”]|”current_page”|<url>|<id>] [context_var=[“tree”]] [current_class=className|[“current”]] [parent_class=className|[“parent”]] [trail_class=className|[“trail”]]

%}