Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Eddie Foreman 215 posts 288 karma points
    Feb 24, 2011 @ 19:30
    Eddie Foreman
    0

    Test if current page has no sub pages.

    Hi All,

    I have the following xslt for the 4.5 schema:

    <?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" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

    <xsl:output method="xml" omit-xml-declaration="yes" />

    <xsl:param name="currentPage"/>
    <xsl:variable name="level" select="2"/>

    <xsl:template match="/">
    <xsl:variable name="scheme" select="string($currentPage/colourScheme)" />
    <!-- The fun starts here -->
    <div class="content-nav"><!-- want to wrap an if statement round this, so that its only displayed if the current page has child nodes -->
    <xsl:if test="$currentPage/ancestor-or-self::* [count(./node) != 0]">
    <xsl:choose>
    <xsl:when test="$scheme = ''">
    <h2>Find out more:</h2>
    </xsl:when>
    <xsl:otherwise>
    <xsl:if test="$scheme = 'local'">
    <h2>Choose your local Authority:</h2>
    </xsl:if>
    <xsl:if test="$scheme = 'community'">
    <h2>Find out more about: </h2>
    </xsl:if>
    <xsl:if test="$scheme = 'about'">
    <h2>Find out more: </h2>
    </xsl:if>
    <xsl:if test="$scheme = 'contact'">
    <h2>Find out more: </h2>
    </xsl:if>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:if>
    <ul class="nav-btns"><!-- not closed if the current page contains no child nodes -->
    <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
    <li>
    <xsl:choose>
    <xsl:when test="position() = 1 and $currentPage/@id = current()/@id">
    <xsl:attribute name="class">first active</xsl:attribute>
    </xsl:when>
    <xsl:otherwise>
    <xsl:choose>
    <xsl:when test="position() = 1">
    <xsl:attribute name="class">first</xsl:attribute>
    </xsl:when>
    <xsl:otherwise>
    <xsl:choose>
    <xsl:when test="$currentPage/@id = current()/@id">
    <xsl:attribute name="class">active</xsl:attribute>
    </xsl:when>
    </xsl:choose>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:otherwise>
    </xsl:choose>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <span>
    <xsl:value-of select="@nodeName"/>
    </span>
    </a>
    </li>
    </xsl:for-each>
    </ul>
    </div>
    </xsl:template>
    </xsl:stylesheet>

    So far so good but I need a little help in resolving the following to points:

    1. The div with id of content nav should only be displayed if the current page has sub pages.
    2. The ul with class nav-btns is not closed if the current page has no sub pages. 

    For item 1, I tried wrapping an if statement around with the following, but the menu items are hidden on all pages.

    <xsl:if test="$currentPage/ancestor-or-self::* [count(./node) != 0]">

    Any help or pointers would be greatly appericated.

    Thanks in advance

    Eddie

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 24, 2011 @ 19:45
    Morten Bock
    1

    You should be fine with just this:

    <xsl:if test="$currentPage/*[@isDoc]">

    When doing a test=, then it will bu true, if the xpath statement has any results. If not, it will be false.

  • Eddie Foreman 215 posts 288 karma points
    Feb 24, 2011 @ 20:13
    Eddie Foreman
    0

    Hi Morten,

    Cool, this works well on the parent page and displays the buttons. However, when I view a sub page the buttons are not displayed.

    Any thoughts on the closing  <ul  class="nav-btns">, when the current page has no sub pages.

    Thanks again,

    Eddie

  • Kim Andersen 1447 posts 2196 karma points MVP
    Feb 24, 2011 @ 22:03
    Kim Andersen
    0

    Hi Eddie

    The not-closing ul: You sure that the ul is actually not just collapsing. If theres no nodes to run through, then the ul will be empty resulting in the element collapsing. Try inserting a

    <li>&nbsp;</li>

    just before the </ul> and outside the for-each loop.

    This would mean that here will always be at least one li-element inside the ul. The li could have a class called something like hide, and then say this in your css:

    .hide{display:none}

    UPDATE: The <li> above should contain a &nbsp;

    /Kim A

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Feb 24, 2011 @ 22:11
    Jan Skovgaard
    1

    Hi Eddie

    I would either do

    1) Make a test to ensure the <ul> is only written to the source if nodes are present - having an empty <ul> in the source does not make any sense at all in my world

    2) If for some reason the above suggestion is not possible to do, then I would insert an <xsl:comment />, which makes sure the <ul> does not collapse and the layout of the page is not messed up.

    3) If you can't get the above to work I would try to change the output method from XML to HTML since the empty <ul> will not collapse when rendered as HTML instead of XML. But this can mess up other parts of the layout in some cases.

    Using a .hide class should be the last thing to try out IMHO :-)

    /Jan

  • Kim Andersen 1447 posts 2196 karma points MVP
    Feb 24, 2011 @ 22:15
    Kim Andersen
    1

    Yeah like Jan says, the best solution probably would be to check to see if there is any nodes before rendering the ul at all. Something like this:

    <xsl:if test="count($currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']) &gt; 0>

    wrapped around the entire ul.

    /Kim A

  • Eddie Foreman 215 posts 288 karma points
    Feb 26, 2011 @ 12:21
    Eddie Foreman
    1

    Hi All,

    Thanks for the all the advice. 

    Ended up puting the check before the first div, which has resolved both of the issues.

    <xsl:if test="count($currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']) &gt; 0">
          <div class="content-nav">
    ...
    </div>
    </xsl:if>

    Thanks again.

    Eddie

Please Sign in or register to post replies

Write your reply to:

Draft