Skip to content

Instantly share code, notes, and snippets.

@cfalzone
Last active August 29, 2015 14:03
Show Gist options
  • Save cfalzone/ca031d3a4e534f1a62f9 to your computer and use it in GitHub Desktop.
Save cfalzone/ca031d3a4e534f1a62f9 to your computer and use it in GitHub Desktop.
DotCMS Pagination Example
#macro(myPagination $currentPage $totalPages)
#if($UtilMethods.isSet($currentPage) && $UtilMethods.isSet($totalPages))
#set($currentPage = $math.toInteger($currentPage))
#set($titalPage = $math.toInteger($totalPages))
#if($currentPage > 0 && $currentPage <= $totalPages)
#set($pageMap = $contents.getEmptyMap())
#set($prev = $math.sub($currentPage, 1))
#set($next = $math.add($currentPage, 1))
#set($nextToLast = $math.sub($totalPages, 1))
## Determine what the 1 link should look like
#if(1 == $currentPage)
#set($_dummy = $pageMap.put(1, "(1)"))
#else
#set($_dummy = $pageMap.put(1, "<a href='?page=1'>1</a>"))
#end
## Determine what the 2 link should look like
#if(2 == $currentPage)
#set($_dummy = $pageMap.put(2, "(2)"))
#else
#set($_dummy = $pageMap.put(2, "<a href='?page=2'>2</a>"))
#end
## Determine if we need the first break
#if($currentPage > 4 && $totalPages > 4) #set($_dummy = $pageMap.put(3, "...")) #end
## Add in the previous page
#if($prev > 2)
#set($_dummy = $pageMap.put($prev, "<a href='?page=${prev}'>${prev}</a>"))
#end
## Add in the curent link
#if($currentPage > 2)
#set($_dummy = $pageMap.put($currentPage, "(${currentPage})"))
#end
## Add in the next link
#if($next <= $totalPages)
#set($_dummy = $pageMap.put($next, "<a href='?page=${next}'>${next}</a>"))
#end
## Determine if we need the second break
#if($nextToLast > $math.add($next,1)) #set($_dummy = $pageMap.put($math.add($next,1), "...")) #end
## Determine what the next to last link should look like
#if($nextToLast == $currentPage)
#set($_dummy = $pageMap.put($nextToLast, "(${nextToLast})"))
#else
#set($_dummy = $pageMap.put($nextToLast, "<a href='?page=${nextToLast}'>${nextToLast}</a>"))
#end
## Determine what the last link should look like
#if($totalPages == $currentPage)
#set($_dummy = $pageMap.put($totalPages, "($totalPages)"))
#else
#set($_dummy = $pageMap.put($totalPages, "<a href='?page=${totalPages}'>${totalPages}</a>"))
#end
## Now print them all out
<div class='pagination'>
#foreach($i in $sorter.sort($pageMap.keySet()))
<span class='paginationItem'>${pageMap.get($i)}</span>
#end
</div>
#end
#end
#end
<h2> Test Pagination </h2>
#set($totalPages = 46)
#foreach($p in [1 .. $totalPages])
<div>
<h3>Pagination where page = $p</h3>
#myPagination($p $totalPages)
<hr />
</div>
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment