Trouble creating a navigation menu to include the parent node and the child nodes
I have a basic site with the content tree as follows;
Homepage > About Us > Products
The xslt macro creates a menu but only lists the child nodes. I could hard code the homepage but that seems to be a hacky approach (not included in code below).
<xsl:param name="currentPage"/>
<!-- Input the documenttype you want here --> <xsl:variable name="level" select="1"/>
@* Macro to display child pages below the root page of a standard website. Also highlights the current active page/section in the navigation with the css class "current". *@
@{ @*Get the root of the website *@ var root = Model.AncestorOrSelf(1); }
<ul> <li><a href="@root.NiceUrl">@root.Name</a></li> @foreach (var page in root.Children.Where("Visible")) {
This would add a class of current to the li of the page I am on.
Seems odd how you are going to need a website with a main navigation using the recommended content structure but there seems to be no clear or accessible solution?
Yeh, it worked. Thank you very much. However, I'm told that XSLT is to be avoided and have been advised to use the following: However, this too now throws up errors?
If you want to do it in Razor, this code should do it for you.
@inherits umbraco.MacroEngines.DynamicNodeContext
@* Macro to display child pages below the root page of a standard website. Also highlights the current active page/section in the navigation with the css class "current". *@
@{ @*Get the root of the website *@ var root = Model.AncestorOrSelf(1);
Aly you are very welcome. I´m glad that I could help you,
I think you should mark this topic as solved so other can see what worked for you. Since you are new to the Umbraco forum, I assume that youdon´t knowhow to dothis.Therefore, I wouldof courseguide you how to do it. Find the person that gives you the correct answer and then click the green tick.
Trouble creating a navigation menu to include the parent node and the child nodes
I have a basic site with the content tree as follows;
Homepage
> About Us
> Products
The xslt macro creates a menu but only lists the child nodes. I could hard code the homepage but that seems to be a hacky approach (not included in code below).
<xsl:param name="currentPage"/>
<!-- Input the documenttype you want here -->
<xsl:variable name="level" select="1"/>
<xsl:template match="/">
<!-- The fun starts here -->
<ul>
<xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
<li>
<a href="{umbraco.library:NiceUrl(@id)}">
<xsl:value-of select="@nodeName"/>
</a>
</li>
</xsl:for-each>
</ul>
</xsl:template>
</xsl:stylesheet>
However, is it better to use cshtml?
@inherits umbraco.MacroEngines.DynamicNodeContext
@*
Macro to display child pages below the root page of a standard website.
Also highlights the current active page/section in the navigation with
the css class "current".
*@
@{
@*Get the root of the website *@
var root = Model.AncestorOrSelf(1);
}
<ul>
<li><a href="@root.NiceUrl">@root.Name</a></li>
@foreach (var page in root.Children.Where("Visible"))
{
<li class="@page.IsAncestorOrSelf(Model, "current", "")">
<a href="@page.Url">@page.Name</a>
</li>
}
</ul>
This would add a class of current to the li of the page I am on.
Seems odd how you are going to need a website with a main navigation using the recommended content structure but there seems to be no clear or accessible solution?
Hi Aly,
Try this code:
I hope this is what you were looking for. Remember to change the homeNode variable so it match your Homepage documenttype alias.
/Dennis
Thanks Dennis. However, it wont let me save the file as it throws up the following errors. Any ideas?
Hi Aly,
The reason why you are getting this error message is because you need to change the homeNode variable so it match your Homepage documenttype alias
In my case it´s Forside (The document type for the frontpage), I have made an screen shot so you can see what you should change:
I hope this helps you.
/Dennis
Yeh, it worked. Thank you very much. However, I'm told that XSLT is to be avoided and have been advised to use the following: However, this too now throws up errors?
var homePage = Model.AncestorOrSelf(1);
<ul>
<li class="@(homePage.Id == Model.Id ? "active" : null)">
<a href="@homePage.Url">@homePage.Name</a>
</li>
@foreach (var page in homePage.Children.Where("Visible"))
{
<li class="@(page.Id == Model.Id ? "active" : null)">
<a href="@page.Url">@page.Name</a>
</li>
}
</ul>
Hi Aly,
If you want to do it in Razor, this code should do it for you.
/Dennis
Great news. That also worked. Now I have a re-usable chunk of code to use for a basic navigation. Thanks very much, You're a star !
Aly you are very welcome. I´m glad that I could help you,
I think you should mark this topic as solved so other can see what worked for you. Since you are new to the Umbraco forum, I assume that you don´t know how to do this. Therefore, I would of course guide you how to do it. Find the person that gives you the correct answer and then click the green tick.
Hope the explanation makes sense.
/Dennis
is working on a reply...