Navigation Macro based on dotCMS' navTool viewtool

Description

This macro extends the functionality provided by dotCMS' navTool viewtool.

It is based on folders and will allow you to set a starting point (root folder or subfolder), how many levels you want to be taking into account to build the navigation, and whether or not the first level should be the only one to be displayed on load.

Parameters:

  1. main_sub [text string] - Define the start point of the navigation
    • "/" pulls from the root, for main navigation
    • "/about/" pulls from the directory, for interior sub navigaton
  2. level_deep [integer] - How many levels should the navigation go
    • 1 get parent level only
    • 2 ~ N gets children levels
  3. open_levels[true/false] - defines whether children folders and pages are displayed
    • true always shows children
    • false only show children when direct parent is active

Usage:

#navTool("/folder/",level deep - number,children nav open? - true or false)

For example:
#navTool("/",1,false) will display only the folders and pages at root level marked as shown on menu.
#navTool("/about-us",3,true) will display the sub-folders and pages marked as shown on menu for the About Us section up to three levels deep. All levels will be shown on load.

Code

#macro(navToolSubmenu $nav_children $num)
  #set($num = $num+1)
  <ul>
    #foreach($nav_item in $nav_children)
      #if(!$nav_item.href.contains("index.dot"))
        #if($UtilMethods.isSet($nav_item.codeLink))
          <li>$nav_item.codeLink</li>
        #else
          #if($UtilMethods.isSet($nav_item.href))
            <li#if($nav_item.active == true) class="active"#end>
              <a href="${nav_item.href}">$nav_item.title</a>
              #if(($nav_item.active == true || $open_levels == true) && ($nav_item.children && $nav_item.children.size() > 0 && $levels_deep > 1))
                #navToolSubmenu($nav_item.children)
              #end
            </li>
          #end
        #end
      #end
    #end
  </ul>
#end

#macro(navTool $main_sub, $levels_deep, $open_levels)
  
  ## DEFAULT IS SET TO Root
  #if(($UtilMethods.isSet($main_sub) && $main_sub == 1) || ($UtilMethods.isSet($main_sub) && $main_sub == "") || !$UtilMethods.isSet($main_sub))
    #set ($main_sub = "main")
  #else
    #set ($main_sub = "$main_sub")
  #end

  ## DEFAULT IS SET TO 1 LEVEL DEEP
  #if((!$UtilMethods.isSet($levels_deep)) || ($UtilMethods.isSet($levels_deep) && $levels_deep == "") || ($UtilMethods.isSet($levels_deep) && $levels_deep == 0))
    #set($levels_deep = 1)
  #elseif($UtilMethods.isSet($levels_deep) && $levels_deep > 5)
    #set($levels_deep = 5)
  #end

  ## SET IF ALL SUB FOLDER AND PAGES SHOULD SHOW ON EVERY PAGE
  ## IF SET TO TRUE, ONLY SHOWS SUB FOLDERS AND PAGES WITHIN A CHILD DIRECTORY
  ## DEFAULT IS FALSE
  #if(!$UtilMethods.isSet($open_levels))
    #set($open_levels = false)
  #elseif($UtilMethods.isSet($open_levels))
    #if($open_levels == "true")
      #set($open_levels = true)
    #elseif($open_levels == "false" || $open_levels == "")
      #set($open_levels = false)
    #end
  #end
  
  #if($main_sub == "main")
    ##GET NAVIGATION LIST, START AT ROOT
    #set($nav_list = $navtool.getNav("/"))
  #elseif($main_sub == "sub")
    ##GET NAVIGATION LIST, START AT LOCAL PARENT DIRECTORY
    #set($nav_current_parent = $VTLSERVLET_URI.split('/').get(1))
    #set($nav_list = $navtool.getNav("/${nav_current_parent}"))
  #else
    ##GET NAVIGATION LIST, START AT ROOT
    #set($nav_list = $navtool.getNav("$main_sub"))
  #end
  
  <!-- New Navigation Macro navTool - v1 -->
  <ul>
    #foreach($nav_item in $nav_list)
    <li id="$nav_item.href.replace("/","")" #if($nav_item.active == true)class="active"#end>
      <a href="$nav_item.href">$!{nav_item.title}</a>
      ##IF LINK HAS CHILDREN
      #if(($nav_item.active == true || $open_levels == true) && ($nav_item.children && $nav_item.children.size() > 0 && $levels_deep > 1))
        #navToolSubmenu($nav_item.children 2)
      #end
    </li>
    #end
  </ul>

  ##*******  REORDER IN EDIT MODE ************##
  #if($EDIT_MODE)
    #set($PUBLISH_PERMISSION = $pageAPI.canUserPublish($HTMLPAGE_IDENTIFIER, false))
    #set($startFromPath = $nav.getStartFromPath(1, $request))
    #set($folder = $folderAPI.findCurrentFolder($startFromPath, $host))
    #set($menuId = $folder.inode)
    <form action="${directorURL}" method="post" name="dot_form_menu_${nav.getFormCount()}" id="dot_form_menu_${nav.getFormCount()}">
      <input type="hidden" name="cmd" value="orderMenu" />
      <input type="hidden" name="path" value="$startFromPath" />
      <input type="hidden" name="hostId" value="${host.identifier}" />
      <input type="hidden" name="pagePath" value="${VTLSERVLET_URI}" />
      <input type="hidden" name="referer" value="${VTLSERVLET_URI}" />
      <input type="hidden" name="startLevel" value="1" />
      <input type="hidden" name="depth" value="2" />
      #if ($PUBLISH_PERMISSION)
        <div class="dotMenuReorder"><a href="javascript:document.getElementById('dot_form_menu_${nav.getFormCount()}').submit();"></a></div>
      #else
        <div class="dotMenuReorder"></div>
      #end
    </form>
  $nav.increaseFormCount()
  #end
  
#end