Rendering & Breadcrumbs
Navigation renders semantic HTML through its companion MarkupNavigation module ($navigation->render() and $navigation->renderBreadcrumbs()) and also gives you full manual control over resolved NavigationTree objects when building bespoke mega-menus or mobile drawers.
Rendering a Menu ($navigation->render())
Section titled “Rendering a Menu ($navigation->render())”<?php
declare(strict_types=1);
namespace ProcessWire;
/** @var Navigation $navigation */$navigation = wire()->navigation;
echo $navigation->render('main', [ 'preset' => 'tailwind', 'maxDepth' => 3, 'renderIcons' => true, 'iconSize' => 18, 'iconClass' => 'w-4 h-4 shrink-0', 'dropdownAria' => true,]);Built-In Presets
Section titled “Built-In Presets”Pass 'preset' as one of four built-in class maps:
'default': Semanticnav-*classes (nav-list,nav-sublist,nav-item,nav-link,nav-heading,nav-divider).'uikit': UIkit-compatible navigation classes.'tailwind': Clean utility-ready Tailwind CSS layout classes.'plain': Bare semantic HTML (<nav>,<ul>,<li>,<a>) with zero preset classes.
All 19 MarkupNavigation::renderTree() Options
Section titled “All 19 MarkupNavigation::renderTree() Options”| Option | Default | Description |
|---|---|---|
preset |
'default' |
'default', 'uikit', 'tailwind', or 'plain'. |
navTag |
'nav' |
Wrapper tag ('nav', 'div', 'aside', 'header', 'footer', 'section', or '' to omit). |
ariaLabel |
$menu->title |
aria-label on semantic landmarks; when navTag => 'div' and an explicit ariaLabel is passed, emits role="navigation" aria-label="...". |
navClass |
Preset | Additional CSS classes on the outer wrapper. |
listClass |
Preset | CSS classes on the root <ul>. |
subListClass |
Preset | CSS classes on nested <ul> dropdowns. |
itemClass |
Preset | CSS classes on every <li>. |
linkClass |
Preset | CSS classes on every <a>. |
activeClass |
'is-active' |
Class added to the active <li>. |
activeTrailClass |
'is-active-trail' |
Class added to ancestor <li> items along the active trail. |
activeLinkClass |
'is-current' |
Class added to the active <a> (alongside aria-current="page"). |
parentClass |
'has-children' |
Class added to <li> items that have child nodes. |
headingClass |
Preset | Class added to passive heading/text <span> elements. |
dividerClass |
Preset | Class added to passive <li role="separator"> dividers. |
dropdownAria |
true |
Emit aria-haspopup="true" and aria-expanded on parent links. |
renderIcons |
false |
Prepend sanitized inline vector <svg> via $node->iconSvg(). |
iconSize |
16 |
Pixel size for inline SVG icons. |
iconClass |
'nav-icon' |
CSS class on rendered <svg> elements. |
maxDepth |
0 (inherits menu) |
Override maximum rendered depth. |
itemCallback |
null |
fn(NavigationNode $node, string $defaultInnerHtml, int $depth): string to decorate or replace an item’s inner HTML. |
Decorating Items With itemCallback
Section titled “Decorating Items With itemCallback”<?php
declare(strict_types=1);
namespace ProcessWire;
use Totoglu\Navigation\Entity\NavigationNode;
echo $navigation->render('main', [ 'preset' => 'tailwind', 'renderIcons' => true, 'itemCallback' => static function (NavigationNode $node, string $defaultHtml, int $depth) use ($sanitizer): string { $badge = (string) $node->getCustom('badge_text', ''); if ($badge === '') { return $defaultHtml; }
return $defaultHtml . '<span class="ml-2 rounded-full bg-emerald-100 px-2 py-0.5 text-xs font-medium">' . $sanitizer->entities($badge) . '</span>'; },]);Custom Tree Traversal ($navigation->tree())
Section titled “Custom Tree Traversal ($navigation->tree())”For mega-menus, custom mobile drawers, or multi-column footers grouped by column_group, fetch the resolved NavigationTree directly (or use the invokable shortcut $navigation('main')):
<?php
declare(strict_types=1);
namespace ProcessWire;
/** @var Page $page *//** @var Sanitizer $sanitizer *//** @var Navigation $navigation */$navigation = wire()->navigation;
$tree = $navigation->tree('main', [ 'currentPage' => $page, 'includeHiddenPages' => false,]);
if (!$tree->isEmpty()) { $active = $tree->activeNode(); $trail = $tree->activeTrail();
foreach ($tree->nodes() as $node) { $subtitle = (string) $node->getCustom('subtitle', ''); $panel = $node->panelHandle; // Non-empty only on root nodes (depth === 0) echo '<a href="' . $sanitizer->entities($node->url) . '">'; echo $node->iconSvg(16, 'inline-icon'); echo $sanitizer->entities($node->title); echo '</a>'; }}Breadcrumbs & Schema.org BreadcrumbList
Section titled “Breadcrumbs & Schema.org BreadcrumbList”Resolve breadcrumbs from a specific menu first (falling back to the ProcessWire page hierarchy when the current page is not in the menu) and optionally emit <script type="application/ld+json">:
<?php
declare(strict_types=1);
namespace ProcessWire;
/** @var Page $page *//** @var Navigation $navigation */$navigation = wire()->navigation;
// Render accessible <nav aria-label="Breadcrumb"> + Schema.org BreadcrumbList JSON-LDecho $navigation->renderBreadcrumbs($page, [ 'menu' => 'main', 'prependHome' => true, 'homeLabel' => __('Home', __FILE__), 'includeCurrent' => true, 'separator' => '/', 'schemaJsonLd' => true,]);
// Or emit standalone BreadcrumbList JSON-LD inside <head>echo $navigation->renderJsonLd($page, ['menu' => 'main']);BreadcrumbBuilder and NavigationBreadcrumbs::toSchemaOrg() automatically filter empty titles, omit non-navigable # intermediary items, and harden JSON output with JSON_HEX_* flags.