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").
<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?
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)
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.
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:
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?
Insert something like this right after your opening <ul>:
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?
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.
Cheers,
/Dirk
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:
is working on a reply...