Autonav template - trouble getting parrent to have $navSelected class when on children

Permalink
Hi There,

I have a custom autonav template for the main menu - I created it by duplicating and modifying another template.

When on the parent page the .current class is applied but when on a child, the current class isn't applied.

Is there a way to show what section you're in by having the autonav template apply the current class to the parent?

Here is my main nav template:
<?php 
   defined('C5_EXECUTE') or die("Access Denied.");
   $aBlocks = $controller->generateNav();
   $c = Page::getCurrentPage();
   $nh = Loader::helper('navigation');
   $isFirst = true;
   foreach($aBlocks as $ni) {
      $_c = $ni->getCollectionObject();
      if (!$_c->getCollectionAttributeValue('exclude_nav')) {
         $target = $ni->getTarget();
         if ($target != '') {
            $target = 'target="' . $target . '"';
         }
         if ($ni->isActive($c) || strpos($c->getCollectionPath(), $_c->getCollectionPath()) === 0) {
            $navSelected='current';


Sorry, just can't seem to work it out.

Cheers

Ben

 
jordanlev replied on at Permalink Reply
jordanlev
Is there a reason you don't just use the default (built-in) template? It includes a "nav-path-selected" class on all parent links. I don't see anything in your custom template that is functionally different from the default template (although I could be wrong about this).
cmscss replied on at Permalink Reply
I think all I did was duplicate the built-in template, rename the class to .current and try to move the class to the <a> element.

Did I remove something else?
jordanlev replied on at Permalink Reply
jordanlev
OOhhhh.... I see what's going on. It looks like you copied the header_menu template, not the default template (the default template has a whole series of nested <ul> and <li> elements -- an entire site hierarchy for dropdown menus basically, whereas what you have is only a single level of menu items).

[EDIT: See response below which is actually correct]
jordanlev replied on at Permalink Reply
jordanlev
Wait, scratch that -- I misunderstood the code. Actually that $navSelected thing *is* being outputted in the header_menu template, but it's being put on the <li> that surrounds the <a> tags, not the <a> tags themselves.

And in your code, you somehow removed the thing that outputs that $navSelected class. So what you want to do is change this:
echo '<li class="'.$isFirstClass.'">';

...back to this:
echo '<li class="'.$navSelected.' '.$isFirstClass.'">';
cmscss replied on at Permalink Reply
Thanks for that.

Actually, what I've done is move the .current class from the <li> to the <a> which works great:
echo('<a class="'.$navSelected.'" href="' . $pageLink . '"  ' . $target . '>' . $ni->getName() . '</a>');


The issue is that when you're on a child page, the .current class doesn't get applied which means the main nav is not displaying were you are in the site.

Maybe this is this less a function of the autonav template and more a function of the autonav parameters in header.php?

header.php looks like this:
<?php
$autonav = BlockType::getByHandle('autonav');
$autonav->controller->orderBy = 'display_asc';
$autonav->controller->displayPages = 'top';
$autonav->render('templates/main_nav');
?>
jordanlev replied on at Permalink Best Answer Reply
jordanlev
You have an "if" statement just before your <a> link that is preventing your modified code from being displayed on any page other than the "current" page. So change this:
if ($c->getCollectionID() == $_c->getCollectionID()) { 
   echo('<a class="'.$navSelected.'" href="' . $pageLink . '"  ' . $target . '>' . $ni->getName() . '</a>');
} else {
   echo('<a href="' . $pageLink . '"  ' . $target . '>' . $ni->getName() . '</a>');
}

...to this:
echo('<a class="'.$navSelected.'" href="' . $pageLink . '"  ' . $target . '>' . $ni->getName() . '</a>');

...and you should be good to go!
cmscss replied on at Permalink Reply
Thanks matey - worked great.

Took me a couple of looks to get rid of the entire ie/else - in the end my fiancee worked it out!