Copied to clipboard

Flag this post as spam?

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


  • Kris Anderson 12 posts 32 karma points
    Jan 09, 2011 @ 05:43
    Kris Anderson
    0

    How to have a link to a parent directory in the sub-directory?

    Let me explain that better because the title may sound a little confusing. I have 2 navigation structures on a site. I have the Main navigation, and then the sub navigation.

    What I'm looking for is to get the main parent navigation link showing at the top of the sub navigation. For example, in my main navigation I have the following:

    Home - About Us - Blog - Contact Us

    Under the About Us main section, the sub-navigation says:

    What We Do
    Our History
    Our Goals

    When you inititially click on "About Us", it takes you to site-name.com/about-us/, and on that page none of the sub-category items are selected. This is working like it should.

    However, I would like to add a link called "About Us" to the top of my sub-navigation, and when you click on "About Us" from the main navigation, both the main navigation and sub-navigation will be selected (class="selected").

    So my sub-nagivation would look like:

    About Us
    What We Do
    Our History
    Our Goals

    My current sub-navigation XLST file is:


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

    <ul>
    <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
      <li>
            <xsl:if test="$currentPage/ancestor-or-self::*/@id = current()/@id">
          <!-- we're under the item - you can do your own styling here -->
          <xsl:attribute name="class">selected</xsl:attribute>
        </xsl:if>
        <a href="{umbraco.library:NiceUrl(@id)}/">
          <xsl:value-of select="@nodeName"/>
        </a>
      </li>
    </xsl:for-each>
    </ul>

    </xsl:template>


    So is there a way to do this? Can I call the parent item and have that put above all my sub-navigation links?

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Jan 09, 2011 @ 10:56
    Morten Bock
    0

    Insert something like this right after your opening <ul>:

    <xsl:variable name="mainNavPage" select="$currentPage/ancestor-or-self::* [@level=$level]"/>
    <li>
         <xsl:if test="$currentPage/@id = $mainNavPage/@id">
          <xsl:attribute name="class">selected</xsl:attribute>
        </xsl:if>
     <a href="{umbraco.library:NiceUrl($mainNavPage/@id}/"><xsl:value-of select="$mainNavPage/@nodeName"/>
    </a>
    </li>
  • Kris Anderson 12 posts 32 karma points
    Jan 10, 2011 @ 23:24
    Kris Anderson
    0

    Hi Morten,

    Thank you for the reply. I modied your code and got this to work:

      <xsl:variable name="mainNavPage" select="$currentPage/ancestor-or-self::* [@level=$level]"/>
      <li>
        <xsl:if test="$currentPage/@id = $mainNavPage/@id">
          <xsl:attribute name="class">selected</xsl:attribute>
        </xsl:if>
        <a href="{umbraco.library:NiceUrl($mainNavPage/@id)}/">
          <xsl:value-of select="$mainNavPage/@nodeName"/>
        </a>

    However, if I try to save that in my SubNavigation.xlst file with the "skip testing" box unchecked, I get the following error:

    Error occured

    System.OverflowException: Value was either too large or too small for an Int32.
    at System.Convert.ToInt32(Double value)
    at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
    at System.Xml.Xsl.Runtime.XmlQueryRuntime.ChangeTypeXsltArgument(XmlQueryType xmlType, Object value, Type destinationType)
    at System.Xml.Xsl.Runtime.XmlQueryContext.InvokeXsltLateBoundFunction(String name, String namespaceUri, IList`1[] args)
    at (XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
    at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
    at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer)
    at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, TextWriter results)
    at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)

    Why is that error popping up?

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jan 10, 2011 @ 23:58
    Dirk De Grave
    0

    Cause you're using a NiceUrl() xslt extension method that requires a integer, and $mainNavPage/@id may not have a value until runtime... So, best practice is to put that specific snippet in a xsl:test, eg.

    <xsl:if test="string($mainNavPage/@id) != ''">
    <href="{umbraco.library:NiceUrl($mainNavPage/@id)}/">
    <xsl:value-of select="$mainNavPage/@nodeName"/>
    </a>
    </xsl:if>

    Cheers,

    /Dirk

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Jan 11, 2011 @ 00:01
    Morten Bock
    0

    Hi Kris.

    Not to worry. It is just because umbraco will try to verify the xslt, but it cannot figure out what the value of $mainNavPage will be, so it does not know that it is an int. You can avoid it by throwing an <xsl:if in there like:

    <xsl:variable name="mainNavPage" select="$currentPage/ancestor-or-self::* [@level=$level]"/>
    <xsl:if test="$mainNavPage">
    <li>
        <xsl:if test="$currentPage/@id = $mainNavPage/@id">
          <xsl:attribute name="class">selected</xsl:attribute>
        </xsl:if>
        <a href="{umbraco.library:NiceUrl($mainNavPage/@id)}/">
          <xsl:value-of select="$mainNavPage/@nodeName"/>
        </a>
    </li>
    </xsl:if>
Please Sign in or register to post replies

Write your reply to:

Draft