Skip to content

Instantly share code, notes, and snippets.

@design-innovations
Last active May 25, 2021 03:46
Show Gist options
  • Save design-innovations/5df644304cc4ee3550369b275714f248 to your computer and use it in GitHub Desktop.
Save design-innovations/5df644304cc4ee3550369b275714f248 to your computer and use it in GitHub Desktop.
Pages - Navigation
This partial can create an unlimited hierarchical menu. The active menu is activated using a active-child class,
the parent and grandparents of the active menu get a child-is-active class.
Getting the items:
Pages
The starting folder can be configured using the $foldervariable, if not defined the root will be used and the
depth using the $levelvariable, if not defined the menu will render the complete pages tree.
<? $items = collection('pages', ['folder' => $folder ?? '.', 'level' => $level ?? null, 'recurse' => 'true']) ?>
Joomla
The menu type can be configured using the $typevariable, if not defined the root will be used and the depth using
the $levelvariable, if not defined the menu will render the complete menu items tree.
<? $items = collection('menus', ['type' => $type ?? 'mainmenu', 'level' => $level ?? null, 'recurse' => 'true']) ?>
The partial
<? $renderMenu = function($items, $level) use(&$renderMenu) { ?>
<? foreach($items as $item):
$item_active = false;
if(url()->getPath() == '/'.$item->path) :
$item_active = true;
endif;
$child_active = false;
if($item->hasChildren()) :
ob_start();
$child_active = $renderMenu($item->getChildren(), $level + 1);
$menu = ob_get_clean();
endif;
$class = $item_active ? 'is-active' : ($child_active ? 'child-is-active' : '');
?>
<li data-level="<?= $level ?>"<?= $class ? ' class="'.$class.'"' : ''?>>
<a href="<?= route($item) ?>" itemprop="url">
<span itemprop="name"><?= $item->name ?></span>
</a>
<? if($item->hasChildren()) : ?>
<ul>
<?= $menu ?>
</ul>
<? endif ?>
</li>
<?
endforeach;
return $item_active ?? false
?>
<? } ?>
<ul class="nav" itemscope itemtype="http://www.schema.org/SiteNavigationElement">
<? $renderMenu($items, 1) ; ?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment