Archive for category XSLT

Paged List of Child Nodes in Umbraco

We just started looking at Umbraco- and so I’m delving into Macro’s – these are the packaged up chunks of functionality which make your site actually do things and either take the form of XSLT files which process and spit out the contents of your database, or dot net controls.

I found an excellent example XSLT that allows you to create a paginated list of child nodes over on Tim Geyssens “nibble” blog, and I’ve modified it ever so slightly to stop the numerical index rendering if the pagecount is less than 2- so thought I would re-post it!


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp "&amp;amp;amp;#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:param name="currentPage"/>
  <xsl:template match="/">

    <xsl:variable name="recordsPerPage" select="/macro/recordsPerPage"/>
    <xsl:variable name="pageNumber" >
      <xsl:choose>
        <!-- first page -->
        <xsl:when test="umbraco.library:RequestQueryString('page') <= 0 or string(umbraco.library:RequestQueryString('page')) = '' or string(umbraco.library:RequestQueryString('page')) = 'NaN'">0</xsl:when>
        <!-- what was passed in -->
        <xsl:otherwise>
          <xsl:value-of select="umbraco.library:RequestQueryString('page')"/>
        </xsl:otherwise>
      </xsl:choose>

    </xsl:variable> &amp;amp;amp;nbsp;

    <xsl:variable name="numberOfRecords" select="count($currentPage/node)"/>

    <!-- The fun starts here -->
    <ul>
      <xsl:for-each select="$currentPage/node [string(data [@alias='umbracoNaviHide']) != '1']">
        <xsl:if test="position() > $recordsPerPage * number($pageNumber) and
position() <= number($recordsPerPage * number($pageNumber) +
$recordsPerPage )">
          <li>
            <a href="{umbraco.library:NiceUrl(@id)}">
              <xsl:value-of select="@nodeName"/>

            </a>
          </li>
        </xsl:if>
      </xsl:for-each>
    </ul>

    <xsl:if test="$pageNumber > 0">
      <a href="?page={$pageNumber -1}">previous </a>
    </xsl:if>

<xsl:if test="($numberOfRecords > $recordsPerPage)">
    <xsl:call-template name="for.loop">
      <xsl:with-param name="i">1</xsl:with-param>
      <xsl:with-param name="page" select="$pageNumber +1"></xsl:with-param>
      <xsl:with-param name="count" select="ceiling(count($currentPage/node)div $recordsPerPage)"></xsl:with-param>
    </xsl:call-template>
</xsl:if>

    <xsl:if test="(($pageNumber +1 ) * $recordsPerPage) < ($numberOfRecords)">
      <a href="?page={$pageNumber +1}">next</a>
    </xsl:if>
  </xsl:template>

  <xsl:template name="for.loop">

    <xsl:param name="i"/>
    <xsl:param name="count"/>
    <xsl:param name="page"/>
    <xsl:if test="$i <= $count - 1">

      <xsl:if test="$page != $i">
        <a href="{umbraco.library:NiceUrl($currentPage/@id)}?page={$i - 1}" >

          <xsl:value-of select="$i" />
        </a>
      </xsl:if>

      <xsl:if test="$page = $i">

        <xsl:value-of select="$i" />

      </xsl:if>
    </xsl:if>
    <xsl:if test="$i <= $count - 1">
      <xsl:call-template name="for.loop">
        <xsl:with-param name="i">
          <xsl:value-of select="$i + 1"/>
        </xsl:with-param>
        <xsl:with-param name="count">
          <xsl:value-of select="$count"/>
        </xsl:with-param>
        <xsl:with-param name="page">
          <xsl:value-of select="$page"/>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

2 Comments