Copied to clipboard

Flag this post as spam?

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


  • jivan thapa 194 posts 681 karma points
    Dec 22, 2011 @ 10:54
    jivan thapa
    0

    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.

    :)

  • Rich Green 2246 posts 4008 karma points
    Dec 22, 2011 @ 10:56
    Rich Green
    0

    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 

  • Sören Deger 733 posts 2844 karma points c-trib
    Dec 22, 2011 @ 10:59
    Sören Deger
    0

    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.

     

  • jivan thapa 194 posts 681 karma points
    Dec 22, 2011 @ 12:04
    jivan thapa
    0

    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.

  • Adi 17 posts 36 karma points
    Dec 22, 2011 @ 14:13
    Adi
    0

    @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>
        }

  • jivan thapa 194 posts 681 karma points
    Dec 22, 2011 @ 19:03
    jivan thapa
    0

    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?

     

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Dec 28, 2011 @ 19:57
    Sebastiaan Janssen
    1

    How about:

    @foreach (var node in Model.AncestorOrSelf().Children.Where("useInNavigation == true") {
         
     <a href="@node.Url" title="@node.contentTitle" >@node.contentTitle</a>

    If you want the subitems as well, you could stick it in a helper:

     

    Here's the menu: @RenderChildren(Model.AncestorOrSelf().Children.Where("useInNavigation == true"))

    @helper RenderChildren(dynamic childNodes) {
    <ul>
    @foreach (var node in childNodes) {      
    <li>
    <a href="@node.Url" title="@node.contentTitle" >@node.contentTitle</a></li>
    @RenderChildren(node.Children)

    </ul>

     

  • Paul Stoker 39 posts 72 karma points c-trib
    Jan 08, 2014 @ 17:52
    Paul Stoker
    0

    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..

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Jan 08, 2014 @ 18:02
    Fuji Kusaka
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft