Back

Flexible Navigation Macro

Description

This macro is designed to wrap the stock navigation() macro such that it will default to the current location depth, with a default depth, both of which can be customized. Output is standard based on stock navigation() macro.

Basic Usage:

#customNavigation()

 

Advanced Usage:

#set($menuLevel = 4)
#set($menuDepth = 3)
#customNavigation() 

Code

## ###############################################################################
## Macro Name: Flexible Navigation Macro
## Version: 1.0
## Author: Michael Fienen
## Email: fienen@gmail.com
## Description: Uses some extra logic to allow you to create custom navigation 
##              from the stock nav macro
##
## Required Parameters:
## - none
##
## Optional Parameters:
## - $menuLevel = What level should navgation start at. Default: 2* (int)
## - $menuDepth = How deep should the navigation pull. Default: 2 (int)
##
## * Note: Defaults to 2 unless current depth level > 2, then it defaults to the
##         current page level.
## ###############################################################################
#macro(customNavigation)
  ## SEE IF THERE IS A USER SUBMITTED MENU LEVEL
  #if($UtilMethods.isSet($menuLevel))
    #set($level = $menuLevel)
  ## OTHERWISE WE'LL CALCULATE IT BASED ON LOCATION
  #else
    ## FIND OUT WHAT THE CURRENT FOLDER DEPTH IS
    #set($level = $listTool.size($request.getRequestURI().split("/")) - 1)
    ## SET A MINIMUM LIMIT ON THE LEVEL 
    #if($level < 2)
      #set($level = 2)
    #end
  #end

  ## SEE IF THERE IS A USER SUBMITTED DEPTH TO RENDER
  #if($UtilMethods.isSet($menuDepth))
    #set($depth = $menuDepth)
  ## OTHERWISE DEFAULT TO TWO LEVELS DEEP 
  #else
    #set($depth = 2)
  #end

  ## RUN NAV MACRO WITH SPECIFIED LEVELS OR DEFAULTS
  #navigation($!level $!depth)
#end    

Have you created code you'd like to share with the dotCMS community?

Share your code Become a Contributor