Extending the pageContent() Macro in dotCMS

// September 19th, 2008 // Software, Tech, Web

Here’s my latest nugget of info to help you extend dotCMS.  This isn’t anything complex by any stretch of the mind, but it should be a little time saver for those of you looking into doing blogs, archives, or anything else where you have several pages of content to flip through.  By default, dotCMS comes with a nice little macro called pageContent() that allows you to pull in a list of content, and get a set grouping of results based on where they fall in the sort order.  The only failing was that you had to write your own way to go back and forth with results.

No longer!  With this simple, handy macro, you can automatically get forward and backward navigation after you’ve written your foreach() loop for the pageContent() results.  It makes use of a few of the variables that are created when you run pageContent().  You can supply your own text for the navigation links, as well as give it a custom class name that will automatically be applied to all the elements so that you can format it as you wish.  The code for the macro itself follows below the example.  Here’s a sample of it in use, and what it looks like:
#set($showPageNum = "true")
#set($pageLinkVar = "year=${yearVar}&")
#pageContentNavigation("$page")

Example of the navigation macro

Example of the navigation macro

Here’s the code to get you going.  Just paste it into your /[$DOTCMSROOT]/dotCMS/WEB-INF/velocity/dotcms_library_ext.vm file, or wherever else you’re loading your custom macros, restart the server (if needed), and get going.  There are any number of ways you can modify it to suit your needs, so go nuts!  You can copy the code below, or download the pageContentNavigation_macro.vm file.

  1. ## Macro:       pageContentNavigation()
  2. ## Author:      Michael Fienen
  3. ## Date:        08.09.19
  4. ## Version:     1.0
  5. ## E-mail:      fienen@gmail.com
  6. ## Website:     http://www.supersatellite.com/
  7. ## Description: Creates a "Previous/Next" style navigation after calling the pageContent() macro
  8. ##
  9. ## Required Parameters:
  10. ##     $currentPage  - Integer value for the current page number.
  11. ##
  12. ## Optional Parameters:
  13. ##     $prevPageTxt  - Text for the previous page link. Default: "« Previous Page"
  14. ##     $nextPageTxt  - Text for the next page link. Default: "Next Page »"
  15. ##     $showPageNum  - Show the page number between the navigation links. Default: "false"
  16. ##     $pageLinkVar  - Any necessary parameters to pass to the page for the query. Should end with an "&".
  17. ##     $pageVarName  - Variable name in the URI (Uniform Resource Identifier) for the page number. Default: "page"
  18. ##     $pageNavClass – Customizable CSS (Cascading Style Sheets) class name for the navigation and its elements. Default: "pageContentNav"
  19. ##
  20. #macro(pageContentNavigation $currentPage)
  21.   #if(!$UtilMethods.isSet($pageVarName))
  22.     #set($pageVarName = "page")
  23.   #end
  24.   #if(!$UtilMethods.isSet($prevPageTxt))
  25.     #set($prevPageTxt = "« Previous Page")
  26.   #end
  27.   #if(!$UtilMethods.isSet($nextPageTxt))
  28.     #set($nextPageTxt = "Next Page »")
  29.   #end
  30.   #if(!$UtilMethods.isSet($pageNavClass))
  31.     #set($pageNavClass = "pageContentNav")
  32.   #end
  33. <div class="${pageNavClass}">
  34.   #if($hasPreviousPage == "true")
  35.     #set($prevPage = $math.sub($currentPage,1))
  36.   <span class="${pageNavClass}Link ${pageNavClass}Prev"><a href="$VTLSERVLET_URI?$!{pageLinkVar}&amp;${pageVarName}=${prevPage}">$prevPageTxt</a></span>
  37.   #end
  38.   #if($showPageNum == "true")
  39.   <span class="${pageNavClass}Num">$currentPage of $totalPages</span>
  40.   #end
  41.   #if($hasNextPage == "true")
  42.     #set($nextPage = $math.add($currentPage,1))
  43.   <span class="${pageNavClass}Link ${pageNavClass}Next"><a href="$VTLSERVLET_URI?$!{pageLinkVar}${pageVarName}=${nextPage}">$nextPageTxt</a></span>
  44.   #end
  45. </div>
  46. #end
Share:
  • Print this article!
  • E-mail this story to a friend!
  • Turn this article into a PDF!
  • RSS
  • Twitter
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Digg
  • Reddit
  • Facebook
  • Google Bookmarks
  • Live
  • NewsVine
  • LinkedIn
  • MySpace
  • FriendFeed

Leave a Reply