getCollectionPath() for 1st and subsequent levels

Permalink
It was suggested that I hard code my main nav because the design and functionality is really complicated, so I put my main navigation in navigation_menu.php and am using <?php $this->inc('elements/navigation_menu.php'); ?> in header.php to include it. This is what some of the markup looks like in navigation_menu.php:

<li class="single <?php echo ($c->getCollectionPath() == '/about') ? 'selected' : ''; ?>">
        <a href="about">About Us</a>
    </li>


Works great. The CSS for "selected" shows up when the user is on that page.

Now I have a sublevel of navigation (actually two, but for simplicity's sake I'll say one). When I use the same setup as above on the second level, the CSS for that level shows the "selected" attribute, but then the one for the main navigation "selected" disappears.

Obviously that's because

($c->getCollectionPath() == '/about')


is a main page, and

($c->getCollectionPath() == '/about/subpage')


is a subpage.

But I can't figure out a way to have both the main nav and the sub nav selected when on a second level. Any help would be great.

stephmars
 
stephmars replied on at Permalink Reply
stephmars
I tried doing this:

($c->getCollectionPath() == '/about' || '/about/subpage')


and it worked. But is there a better way?
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Yup, you can use the View::section() function, like this:
<?php
if (View::section('/about')) {
  //The current page is somewhere under the "about" page
} else {
  //The current page is *not* under the "about" page
}
?>
stephmars replied on at Permalink Reply
stephmars
Unfortunately it's not working. I know I'm doing something wrong. I have this:

<li class="single 
        <?php
   if (View::section('/about')) {
     print 'selected';
   } else {
   }
   ?>">
<a href="/about">About</a>
</li>


Any suggestions?
stephmars replied on at Permalink Reply
stephmars
Nevermind, got it! Just had to get rid of the forward slash.

<?php
   if (View::section('about')) {
     print 'selected';
   } else {
   }
   ?>


Thanks so much!! Works great.