Skip to content

Instantly share code, notes, and snippets.

@leekelleher
Created August 3, 2011 08:08
Show Gist options
  • Save leekelleher/1122148 to your computer and use it in GitHub Desktop.
Save leekelleher/1122148 to your computer and use it in GitHub Desktop.
Generic XSLT template for pagination with Umbraco
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp "&#x00A0;">
]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library"
exclude-result-prefixes="msxml umbraco.library">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:variable name="resultsPerPage" select="number(5)" />
<xsl:template name="pagination">
<xsl:param name="page" />
<xsl:param name="matchedNodes" />
<xsl:param name="qs" />
<ul class="pager">
<!-- previous page -->
<xsl:if test="$page &gt; 1">
<li class="pager-prev">
<a href="{concat('?page=', $page - 1, $qs)}" title="Previous page">prev</a>
</li>
</xsl:if>
<!-- each paged set of results is listed, with a link to that page set -->
<xsl:call-template name="pageNumbers">
<xsl:with-param name="pageIndex" select="1" />
<xsl:with-param name="page" select="$page" />
<xsl:with-param name="matchedNodes" select="$matchedNodes" />
<xsl:with-param name="qs" select="$qs" />
</xsl:call-template>
<!-- next page -->
<xsl:if test="$page * $resultsPerPage &lt; count($matchedNodes)">
<li class="pager-next">
<a href="{concat('?page=', $page + 1, $qs)}" title="Next page">next</a>
</li>
</xsl:if>
</ul>
</xsl:template>
<xsl:template name="pageNumbers">
<xsl:param name="page" />
<xsl:param name="pageIndex" />
<xsl:param name="matchedNodes" />
<xsl:param name="qs" />
<xsl:variable name="distanceFromCurrent" select="$pageIndex - $page"/>
<!-- show a maximum of nine paged sets on either side of the current paged set; just like Google does it -->
<xsl:if test="($distanceFromCurrent &gt; -6 and $distanceFromCurrent &lt; 6)">
<xsl:choose>
<xsl:when test="$pageIndex = $page">
<li class="pager-current">
<xsl:value-of select="$pageIndex" />
</li>
</xsl:when>
<xsl:otherwise>
<li class="pager-item">
<a href="{concat('?page=', $pageIndex, $qs)}" title="Page {$pageIndex}">
<xsl:value-of select="$pageIndex" />
</a>
</li>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
<!-- recursively call the template for all the paged sets -->
<xsl:if test="$pageIndex * $resultsPerPage &lt; count($matchedNodes)">
<xsl:call-template name="pageNumbers">
<xsl:with-param name="pageIndex" select="$pageIndex + 1" />
<xsl:with-param name="page" select="$page" />
<xsl:with-param name="matchedNodes" select="$matchedNodes" />
<xsl:with-param name="qs" select="$qs" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp "&#x00A0;">
]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library"
exclude-result-prefixes="msxml umbraco.library">
<xsl:output method="xml" omit-xml-declaration="yes" />
<xsl:include href="_pagination.xslt"/>
<xsl:param name="currentPage"/>
<xsl:variable name="page" select="umbraco.library:RequestQueryString('page')" />
<xsl:template match="/">
<xsl:variable name="matchedNodes" select="$currentPage/descendant::*[@isDoc]" />
<xsl:variable name="nodeCount" select="count($matchedNodes)" />
<xsl:if test="$nodeCount &gt; 0">
<xsl:variable name="page">
<xsl:choose>
<!-- first page -->
<xsl:when test="number(umbraco.library:RequestQueryString('page')) &lt;= 1 or string(umbraco.library:RequestQueryString('page')) = '' or string(number(umbraco.library:RequestQueryString('page'))) = 'NaN'">
<xsl:value-of select="number('1')" />
</xsl:when>
<!-- last page -->
<xsl:when test="number(umbraco.library:RequestQueryString('page')) &gt; $nodeCount div $resultsPerPage">
<xsl:value-of select="ceiling($nodeCount div $resultsPerPage)" />
</xsl:when>
<!-- the value specified in the url -->
<xsl:otherwise>
<xsl:value-of select="number(umbraco.library:RequestQueryString('page'))"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="startMatch" select="($page - 1) * $resultsPerPage + 1" />
<xsl:variable name="endMatch">
<xsl:choose>
<!-- all the rest (on the last page) -->
<xsl:when test="($page * $resultsPerPage) &gt; $nodeCount">
<xsl:value-of select="$nodeCount" />
</xsl:when>
<!-- only the appropriate number for this page -->
<xsl:otherwise>
<xsl:value-of select="$page * $resultsPerPage" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="$matchedNodes">
<xsl:apply-templates select="$matchedNodes">
<xsl:sort select="@createDate" order="descending" data-type="text" />
<xsl:with-param name="startMatch" select="$startMatch" />
<xsl:with-param name="endMatch" select="$endMatch" />
</xsl:apply-templates>
</xsl:if>
<xsl:if test="$nodeCount &gt; $resultsPerPage">
<xsl:call-template name="pagination">
<xsl:with-param name="page" select="$page" />
<xsl:with-param name="matchedNodes" select="$matchedNodes" />
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:template>
<xsl:template match="*[@isDoc]">
<xsl:param name="startMatch" />
<xsl:param name="endMatch" />
<xsl:if test="position() &gt;= $startMatch and position() &lt;= $endMatch">
<h3>
<a href="{umbraco.library:NiceUrl(@id)}">
<xsl:value-of select="@nodeName" />
</a>
</h3>
<p>
<xsl:value-of select="umbraco.library:TruncateString(umbraco.library:StripHtml(bodyText), 175, '...')" disable-output-escaping="yes"/>
</p>
<hr />
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment