Selectively Adding Items to Multiple Auto-Nav Blocks

Permalink
This was a problem I ran into and while I managed to solve it, I was curious if there was a more elegant solution. This is purely for posterity and documentation so please don't rush to answer.

The goal was to have a method of adding pages to an auto-nav block based on a page attribute. In this case the page attribute is 'is_header_nav'. I know I could have used 'exclude_from_nav' but using that would remove it all auto-nav blocks. I intend to also have a custom block template to handle footer nav items ('is_footer_nav') and that navigation may have items that are included in the header nav and may have items that are excluded from the header nav.

The biggest obstacle was finding that the subpages (for a dropdown menu) of what I wanted included were being included in the attribute check (which messed things up really well). I wanted a solution, however that would exclude the subpages based on their parent without going through the hassle of adding this attribute to every subpage.

Thoughts?

//*** Step 2 of 2: Output menu HTML ***/
if (count($navItems) > 0) {
    echo '<ul class="nav">'; //opens the top-level menu
   $includeme = false;
    foreach ($navItems as $ni) {
     if ($ni->level == 1) {
        if ($ni->cObj->getAttribute('is_header_nav')){       
         $includeme = true; 
      } else {
         $includeme = false;
      }
     }
     if ($includeme == true){        
      echo '<li class="' . $ni->classes . '">'; //opens a nav item
        echo '<a href="' . $ni->url . '" target="' . $ni->target . '" class="' . $ni->classes . '">' . $ni->name . '</a>';