How to get submenus to appear in autonav template

Permalink
I have built a site template where I am wanting submenus to display for the autonav block. I am unsure how to add submenus to display in an autonav template.

I have hardcoded the autonav block to header.php with the following:

<?php
    $bt_main = BlockType::getByHandle('autonav');
    $bt_main->controller->displayPages = 'top';
    $bt_main->controller->orderBy = 'display_asc';                    
    $bt_main->controller->displaySubPages = 'all';
    $bt_main->controller->displaySubPageLevels = 'all';                      
    $bt_main->render('templates/mytemplate_menu.php);
        ?>


I then have the following in mytemplate_menu.php

<?php  defined('C5_EXECUTE') or die(_("Access Denied."));
$navItems = $controller->getNavItems();
?>
<ul id="nav">
<li><a href="/index.php"><span class="home_icon"></span></a></li>
<?php  foreach ($navItems as $ni) {
   $classes = array();
   if ($ni->isCurrent) {
      $classes[] = 'nav-selected';
   }
   if ($ni->inPath) {
      $classes[] = 'active';
   }
   if ($ni->isFirst) {
      $classes[] = 'first';


I need to add code to allow the submenu items to display (nested list) in this format:
<div id="menu">
<ul>
<li><a href="index.php">Home<span></span></a></li>
<li><a href="/services">Services<span></span></a>
   <div>
   <ul>
                             <li><a href="/services/services1"><span>Services 1</span></a></li>
   <li><a href="/services/services2"><span>Services 2</span></a></li>
                             <li><a href="#" class="last_submenu_item"><span>Multi-Level Menu</span></a>
                              <div class='subsub_menu'>
                                     <ul>
                                      <li class=""><a href="#"><span>Item One</span></a></li>
                                      <li class=""><a href="#" class="last_submenu_item"><span>Item Two</span></a>
                                         <div class='subsubsub_menu'>
                                              <ul>


Any assistance in what I need to add to my autonav template would be greatly appreciated.

Thanks

digievo
View Replies: View Best Answer
ronyDdeveloper replied on at Permalink Reply
ronyDdeveloper
You need to change the mytemplate_menu.php page. Here is the code.

<?php  defined('C5_EXECUTE') or die(_("Access Denied."));
$navItems = $controller->getNavItems();
?>
<ul id="nav">
<li><a href="/index.php"><span class="home_icon"></span></a></li>
<?php  foreach ($navItems as $ni) {
   $classes = array();
   if ($ni->isCurrent) {
      $classes[] = 'nav-selected';
   }
   if ($ni->inPath) {
      $classes[] = 'active';
   }
   if ($ni->isFirst) {
      $classes[] = 'first';
digievo replied on at Permalink Reply
digievo
Thanks ronyDdeveloper.

I am getting the following error:
Parse error: syntax error, unexpected '<' in /hsphere/local/home/domain/domain.com/blocks/autonav/templates/header_menu_4d.php on line 35

This coincides with:

<?php  } ?>
</ul>


Thanks
ronyDdeveloper replied on at Permalink Reply
ronyDdeveloper
Ohhh! I'm sorry. I've forgotten to close the php tag.

Just put
?>
tag before line 35 that is
<?php } ?> </ul>


Rony
digievo replied on at Permalink Reply
digievo
Thanks again for your help.

That fixed the error however it is not displaying the submenu code.

It is is displaying as:

<li class="first">
      <a class="first" href="/" target="_self">Home</a>
   </li> 
   <li class="sub-menu">
      <a class="sub-menu" href="/about/" target="_self">About</a>
   </li> 
   <li class="first">
      <a class="first" href="/about/our-people/" target="_self">Our People</a>
   </li>


Instead of:

<li class="first">
      <a class="first" href="/" target="_self">Home</a>
   </li> 
   <li>
      <a class="sub-menu" href="/about/" target="_self">About</a>
<div>
   <ul>
   <li>
      <a href="/about/our-people/" target="_self"><span>Our People</span></a>
   </li>
 </ul>
</div>


It appears to be ignoring:

<?php
   if ($rs->hasSubmenu) {
      echo '<div><ul>'; 
   } else {
      echo '</li>'.str_repeat('</ul></div></li>', $rs->subDepth); 
   }
?>


Thanks again. I really appreciate your help.
ronyDdeveloper replied on at Permalink Best Answer Reply
ronyDdeveloper
The object was not same as others.

***$rs->hasSubmenu [wrong]
***$ni->hasSubmenu [right]
<?php
   if ($ni->hasSubmenu) {
      echo '<ul>'; 
   } else {
      echo '</li>'.str_repeat('</ul></li>', $rs->subDepth); 
   }
?>


Rony
digievo replied on at Permalink Reply
digievo
Thanks again.

That fixed the problem. I changed the code to:
<?php
   if ($ni->hasSubmenu) {
      echo '<div><ul>'; 
   } else {
      echo '<li>'.str_repeat('</ul></div></li>', $ni->subDepth); 
   }
?>


Is there anyway of hiding the third level items? The site in question has third and fourth level items. That are displaying. I am wanting to display these in a sidebar autonav so do not want to set them as hide in autonav in page properties.

Thanks
ronyDdeveloper replied on at Permalink Reply
ronyDdeveloper
I'm not sure whether it is 100% correct as I've not tested it, but I think this piece of code will work for you.

<?php  defined('C5_EXECUTE') or die(_("Access Denied."));
$navItems = $controller->getNavItems();
?>
<ul id="nav">
<li><a href="/index.php"><span class="home_icon"></span></a></li>
<?php  foreach ($navItems as $ni) {
   $classes = array();
   if ($ni->isCurrent) {
      $classes[] = 'nav-selected';
   }
   if ($ni->inPath) {
      $classes[] = 'active';
   }
   if ($ni->isFirst) {
      $classes[] = 'first';


Rony
digievo replied on at Permalink Reply
digievo
Thanks again.

No it didn't work.

I have just set the autonav to:

Display pages: top level
Subpages to display: all
Sub-page Levels: display a custom amount
levels: 1.

Thanks again for your very generous help. I now have the ability to add a menu with submenus (single level).
digievo replied on at Permalink Reply
digievo
For those that may find it helpful the complete code for the autonav template with submenu items is:

<?php  defined('C5_EXECUTE') or die(_("Access Denied."));
$navItems = $controller->getNavItems();
?>
<ul id="nav">
<li><a href="/index.php"><span class="home_icon"></span></a></li>
<?php  foreach ($navItems as $ni) {
   $classes = array();
   if ($ni->isCurrent) {
      $classes[] = 'nav-selected';
   }
   if ($ni->inPath) {
      $classes[] = 'active';
   }
   if ($ni->isFirst) {
      $classes[] = 'first';