Menus and navigation
This page is for designers who build the menus of a site. Menus are content: editors add a folder at the top level and it appears in the menu. The template fetches the menu items and marks the current page.
Contents
The main menu
{* pagelayout/menu.tpl *}
{def $root_node_id = ezini( 'NodeSettings', 'RootNode', 'content.ini' )
$menu_items = fetch( 'content', 'list', hash( 'parent_node_id', $root_node_id,
'sort_by', array( 'priority', true() ),
'class_filter_type', 'include',
'class_filter_array', array( 'folder', 'frontpage', 'landing_page' ),
'limit', 8 ) )}
<nav class="main-nav" aria-label="'Main navigation'|i18n( 'design/mytheme' )">
<ul>
foreach $menu_items as $item
<li><a href=$item.url_alias|ezurl>$item.name|wash</a></li>
/foreach
</ul>
</nav>
undef $root_node_id $menu_itemscontent.ini [NodeSettings] RootNode is the root of the siteaccess's tree (the starter pagelayout reads it the same way).
Use the class identifiers of your installation.
Marking the current page
Compare with the current page's path, which the pagelayout has in $module_result.path:
def $current_path_ids = array()
if is_array( $module_result.path )
foreach $module_result.path as $stepif is_set( $step.node_id )set $current_path_ids = $current_path_ids|append( $step.node_id )/if/foreach
/if
...
<liif $current_path_ids|contains( $item.node_id ) class="is-active"/if>
<a href=$item.url_alias|ezurlif eq( $item.node_id, first_set( $module_result.node_id, 0 ) ) aria-current="page"/if>$item.name|wash</a>
</li>aria-current="page" tells screen readers which item is the current page; the class styles the active branch.
A second level
Fetch the children of the active top item only, not of every item:
foreach $menu_items as $item
if $current_path_ids|contains( $item.node_id )
def $sub_items = fetch( 'content', 'list', hash( 'parent_node_id', $item.node_id, 'sort_by', $item.sort_array, 'limit', 12 ) )
...
undef $sub_items
/if
/foreachCaching the menu
The menu is the same for everyone with the same roles on the same page:
cache-block keys=array( $module_result.uri, $current_user.role_id_list|implode( ',' ) ) subtree_expiry=$root_node_id
include uri='design:pagelayout/menu.tpl'
/cache-block(Cache-block). Menus of the ezwebin family are configured with menu.ini [SelectedMenu] instead
(The ezwebin design as a base).
Small screens
A menu button with aria-expanded and aria-controls, toggled by a script file, never by inline code
(No inline JavaScript); the menu stays usable without the script (for example
a list of links that the script collapses).
Related pages
- Breadcrumbs
- Fetching nodes and lists
- Keyboard and focus
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.