Skip to content

Instantly share code, notes, and snippets.

@fritzmg
Created July 13, 2015 10:26
Show Gist options
  • Save fritzmg/61e80f42f126f530dcac to your computer and use it in GitHub Desktop.
Save fritzmg/61e80f42f126f530dcac to your computer and use it in GitHub Desktop.
Contao nav_default Template, which allows you to switch to the same subpage within a different "category". Simply create a new custom navigation module, select the pages that act as "category roots" and assign this template. You also need to use folder URLs and the page aliases must be the same (except for the first part, i.e. the "category").
<?php global $objPage; ?>
<ul class="<?= $this->level ?>" role="<?= ($this->level == 'level_1') ? 'menubar' : 'menu' ?>">
<?php foreach ($this->items as $item): ?>
<?php
// get the current page's alias and remove its first part
$arrAlias = explode('/', $objPage->alias);
array_shift($arrAlias);
// create target alias for switch
$strTarget = $item['alias'] . ($arrAliases? '/' . implode('/', $arrAliases) : '');
$objTarget = \PageModel::findByAlias($strTarget);
// check if target exists
if ($objTarget === null)
continue;
// create new href
$item['href'] = \Controller::generateFrontendUrl($objTarget->row());
?>
<?php if ($item['isActive'] || in_array('trail', explode(' ', $item['class']))): ?>
<li class="<?= $item['class'] ?>"><span class="<?= $item['class'] ?>" role="menuitem"<?php if (!empty($item['subitems'])): ?> aria-haspopup="true"<?php endif; ?>><?= $item['link'] ?></span></li>
<?php else: ?>
<li<?php if ($item['class']): ?> class="<?= $item['class'] ?>"<?php endif; ?>><a href="<?= $item['href'] ?>" title="<?= $item['pageTitle'] ?: $item['title'] ?>"<?php if ($item['class']): ?> class="<?= $item['class'] ?>"<?php endif; ?><?php if ($item['accesskey'] != ''): ?> accesskey="<?= $item['accesskey'] ?>"<?php endif; ?><?php if ($item['tabindex']): ?> tabindex="<?= $item['tabindex'] ?>"<?php endif; ?><?php if ($item['nofollow']): ?> rel="nofollow"<?php endif; ?><?= $item['target'] ?> role="menuitem"<?php if (!empty($item['subitems'])): ?> aria-haspopup="true"<?php endif; ?>><?= $item['link'] ?></a></li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment