My umb running site's content structure looks like bellow in image. Each Items at Level 1 is top level navigation. I am currently using XSLT to create top level navigation. I would like to replace it using Razor. I am new in Razor.
In Umbraco go to the Developer section, open the tree "Sripting Files" and create a new razor scripting file. Here you can choose a template, i.e. Navigation.
thanks Adi for your time. But it did not work for me.
"Model.Children.Where("Visible").Count()" selects children nodes but in my content there is not child nodes except for "Education" node.
I guess
- first I have to select all nodes "Home", "Education" "Product", "Blog", "Contact" after that I need looping. I can do this easily using XSLT ($currentPage/ancestor-or-self::* /Content) but it becomes very hard using Razor.
AncestorOrSelf(int) returns the ancestor (or self) of a node at the given level in the argument where as AncestorsOrSelf() is all of the ancestors of this page in the tree.
How to create top level navigation using Razor
My umb running site's content structure looks like bellow in image. Each Items at Level 1 is top level navigation. I am currently using XSLT to create top level navigation. I would like to replace it using Razor. I am new in Razor.
<<<<<<<<<<<<<<<<< XSLT>>>>>>>>>>>>>>>>>>>.
<xsl:variable name="items" select="$currentPage/ancestor-or-self::* /Content [string(useInNavigation) = '1'] "/>
<xsl:for-each select="$items">
<a href="{umbraco.library:NiceUrl(@id)}" title="{contentTitle}" >
<xsl:value-of select="contentTitle"/> </a>
</xsl:for-each>
<>>>>>>>>>>>>>>>>>XSLT>>>>>>>>>>>>>>>>>>>>
How can i loop each items at leve 1 and generate top level navigation using Razor?
Thank you very much.
:)
Hi,
If you go to the Developer section and add a Razor script file you will see a pre defined template for Nagivation, choose this and you should be good.
Hope that helps
Rich
There are a example for this:
In Umbraco go to the Developer section, open the tree "Sripting Files" and create a new razor scripting file. Here you can choose a template, i.e. Navigation.
Thank you very much for your time.
I created Macro Parameter with the alias of "Level" and used Navigation template. its not working for me.
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var level = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.Level);
var ulClass = String.IsNullOrEmpty(Parameter.UlClass) ? "" : String.Format(" class=\"{0}\"", Parameter.UlClass);
var parent = @Model.AncestorOrSelf(level);
if (parent != null) {
<[email protected](ulClass)>
@foreach (var item in parent.Children.Where("Visible")) {
var selected = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
<[email protected](selected)>
<a href="@item.Url">@item.Name</a>
</li>
}
</ul>
}
}
what should i modify to work in my case ?
if I use @level, its out put value 1.
@using System;
@using System.Web;
@using umbraco.NodeFactory;
@if (Model.Children.Where("Visible").Count() > 0)
{
<table class='subpages' cellpadding='0' cellspacing='0'>
<tr><th>In this section</th></tr>
<tr><td style='padding-left: 10px;'><ul class='sb_menu'>
@foreach (var subpage in Model.Children.Where("Visible"))
{
<li><a href="@subpage.Url">@subpage.Name</a></li>
}
</ul></td></tr>
</table>
}
thanks Adi for your time. But it did not work for me.
"Model.Children.Where("Visible").Count()" selects children nodes but in my content there is not child nodes except for "Education" node.
I guess
- first I have to select all nodes "Home", "Education" "Product", "Blog", "Contact" after that I need looping. I can do this easily using XSLT ($currentPage/ancestor-or-self::* /Content) but it becomes very hard using Razor.
Any suggesstion?
How about:
If you want the subitems as well, you could stick it in a helper:
hi Seb, Would this be the best way to create the same top level menu (Umbraco 6.1.6) if you were on any page?
I don't understand how AncestorOrSelf() would help if you were in a sub page and you wanted the main menu?
Hope you can help.
Ta..
Hi Paul,
AncestorOrSelf(int) returns the ancestor (or self) of a node at the given level in the argument where as AncestorsOrSelf() is all of the ancestors of this page in the tree.
Have a look those post.
http://our.umbraco.org/documentation/Reference/Querying/DynamicNode/Collections
http://stackoverflow.com/questions/19916196/umbraco-ancestororselfint-what-does-it-do
//Fuji
is working on a reply...