Wrapping autonav children

Permalink
I am trying to wrap a div in an autonav block just around the lists below parent elements. Currently, I believe the code is working to insert the first tag but the last one is being a hassle. Below is the current template file:
echo '<li class="' . $classes . '">';
   echo '<a class="' . $classes . '" href="' . $ni->url . '" target="' . $ni->target . '">' . $ni->name . '</a>';
   if ($ni->hasSubmenu) {
      if ($ni->level == 1) {
         echo '<div>';
      }
      echo '<ul>';
   } else {
      echo '</li>';
      for ($i = 0; $i < $ni->subDepth; $i++) {
         echo '</ul>';
         if ($ni->subDepth == 2) {
            echo '</div>';
         }
         echo '</li>';


My theory is that the for loop isn't acting correctly and perhaps needs to use $ni->subDepth < x or something of that sort. And/or, the if statement testing for the closing div needs a different check to confirm that it is the last element before the next parent. However, testing and debugging so far have not lead to any results.

Something I noticed during debugging, the menu I am using has 4 parents with children and grandchildren for each except the last. The first and second parent both have a subDepth of 2 on each of the last children. The third has a subDepth of 1 and the fourth 0.


Any ideas?

alivetech
 
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
You are testing $ni->level for the opening DIV, but $ni->subDepth for closing /DIV, so they won't match up.
alivetech replied on at Permalink Reply
alivetech
I don't believe I should be using $ni->level again as this is referring to the $ni-subDepth for loop. The complicated idea here is that I need to mimic the function of
echo str_repeat('</ul></li>', $ni->subDepth);


But on the final call insert a </div> in between the </ul> and </li>

Any tips would be greatly appreciated.
alivetech replied on at Permalink Reply
alivetech
Thanks JohntheFish, after quite a bit more debugging I thought back to your answer and you were right. For anyone interested, here's the final code:
echo '<li class="' . $classes . '">';
   echo '<a class="' . $classes . '" href="' . $ni->url . '" target="' . $ni->target . '">' . $ni->name . '</a>';
   if ($ni->hasSubmenu) {
      if ($ni->level == 1) {
         echo '<div>';
      }
      echo '<ul>';
   } else {
      echo '</li>';
      for ($i = 0; $i < $ni->subDepth; $i++) {
         echo '</ul>';
         if (($i+2) == $ni->level) {
            echo '</div>';
         }
         echo '</li>';