I’ve noticed how most of the Sitecore blogs takes up relatively advanced topics on Sitecore, so I’ve decided to share with you some of my more simpler tricks.
This article is meant as a tutorial. This is a small XSLT I’ve done for several projects that will display a Google style paging selector for any list. Imagine you have a list with 200 items. Displaying all items on one page can slow down your page and make it cluttered. Instead we add a paging component (like Google does it) to the top of the list, and display only 10 items from the list at a time. When clicking the pager, the same page will be called with a parameter ?page=n. The list uses the querystring to determine which elements from the list to display.
Lets begin with the component. First I create a new xsl:template called tplPaging:
<xsl:template name="tplPaging">
<!-- Identifies the number of items in the list -->
<xsl:param name="numberOfItems" />
<!-- Optional parameter identifying the default selected page. Default is 1 -->
<xsl:param name="currentPage" select="sc:qs('page','1')" />
... <!-- more code here -->
</xsl:template>
The template needs 2 parameters, the length of the list (total number of items), and the current page. The current page value can be taken from the querystring, and the default value is page one. So if nothing is defined, we will start with page 1.
Then I need 3 variables: Total number of pages, the start page and the end page. In this case I will only draw 5 pages before and after the current page. Google draws 10, but my lists are shorter, so I’ll stick to 5.
<!-- Calculate the maximum number of pages to show in the paging component --> <xsl:variable name="numberOfPages" select="floor((number($numberOfItems)-1) div 10)+1"/> <!-- Calaulate the starting position of the numbers --> <xsl:variable name="startPage"> <xsl:choose> <xsl:when test="$currentPage > 6"> <xsl:value-of select="$currentPage - 5"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="1"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <!-- Calculate the ending position of the numbers --> <xsl:variable name="endPage"> <xsl:choose> <xsl:when test="$numberOfPages - $currentPage > 5"> <xsl:value-of select="$currentPage + 5"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$numberOfPages"/> </xsl:otherwise> </xsl:choose> </xsl:variable>
Now I can recursively draw the numbers in the paging component. I’ll show you the recursive template later. First the code to call the recursive loop:
<!-- Recursively draw the paging component -->
<ul>
<xsl:if test="$startPage > 1">
<li>
<a href="?page={$currentPage - 6}">Previous</a></li>
</xsl:if>
<xsl:call-template name="tplNumber">
<xsl:with-param name="current" select="$currentPage"/>
<xsl:with-param name="number" select="$startPage"/>
<xsl:with-param name="max" select="$endPage"/>
</xsl:call-template>
<xsl:if test="$currentPage + 5 < $numberOfPages">
<li>
<a href="?page={$currentPage + 6}">Next</a></li>
</xsl:if></ul>
Notice how I do not draw the Previous and the Next unless they are needed?
The template tplNumber is called to draw the actual numbers:
<xsl:template name="tplNumber">
<xsl:param name="current"/>
<xsl:param name="number"/>
<xsl:param name="max"/>
<xsl:choose>
<xsl:when test="$number = $current">
<!-- Show current page without a link -->
<li class="current">
<xsl:value-of select="$number"/>
</li>
</xsl:when>
<xsl:otherwise>
<li>
<a href="?page={$number}"><xsl:value-of select="$number"/></a>
</li>
</xsl:otherwise>
</xsl:choose>
<!-- Recursively call the template untill we reach the max number of pages -->
<xsl:if test="$number < $max">
<xsl:call-template name="tplNumber">
<xsl:with-param name="current" select="$current"/>
<xsl:with-param name="number" select="$number+1"/>
<xsl:with-param name="max" select="$max"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
You can expand this function. What should the pager do if there is no items in the list? Anyway, the examle is actually very simple and shows how easy it is to do paging. And by the way – Sitecore will actually render this blindingly fast.