Copied to clipboard

Flag this post as spam?

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


  • Niels 63 posts 119 karma points
    Apr 02, 2012 @ 15:38
    Niels
    0

    Problem with multilevel navigation (newbie Razor)

    I have created a multilevel navigation with the following wiki: http://our.umbraco.org/wiki/reference/code-snippets/razor-snippets/nested-navigation-with-start-and-finish-levels

    But I want to use a home button, so I used the following code:

    @inherits umbraco.MacroEngines.DynamicNodeContext
                                                          
    @{
      var startLevel = String.IsNullOrEmpty(Parameter.Level) ? 2 : int.Parse(Parameter.StartLevel);
      var finishLevel = String.IsNullOrEmpty(Parameter.Level) ? 3 : int.Parse(Parameter.FinishLevel);   
      var parent = @Model.AncestorOrSelf(startLevel);
      if (parent != null) { @traverse(parent,startLevel,finishLevel) ; }
    }                                                     
                                                       
    @helper traverse(dynamic parent,int startLevel,int finishLevel)
    {
     <ul>
      <li><a href="/">Home</a></li>
        @foreach (var node in parent.Children.Where("Visible")) {
          var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
          <li@Html.Raw(selected)>
            <a href="@node.Url">@node.Name</a>                                       
            @if (@node.Level<=finishLevel) { @traverse(node,startLevel,finishLevel); }  
          </li>
          }
        </ul>                                                            
    }

    But when I look at the navigation, the home button appears in all subnavs like this:
    Home
    Photos
      |_Home
      |_2010
      |_2012
    News
      |_Home
      |_New website
      |_etc

    I want to have 1 home button only, somebody have a clue about this one?

     

  • Douglas Ludlow 210 posts 366 karma points
    Apr 02, 2012 @ 16:16
    Douglas Ludlow
    0

    This is a recursive method and therefore the "Home" link will be added in each time the method is called, which is once for every set of child nodes. Probably the easiest way to not include it here, making minimal changes to your code, would be to include a conditional that checks the level before adding the "home" node:

    @helper traverse(dynamic parent,int startLevel,int finishLevel)
    {
     <ul>
      @if (node.Level == startLevel) { <li><a href="/">Home</a></li> }
      @foreach (var node in parent.Children.Where("Visible"))
      {
        var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
        <li@Html.Raw(selected)>
          <a href="@node.Url">@node.Name</a>                                       
          @if (node.Level <= finishLevel) { @traverse(node,startLevel,finishLevel); }  
        </li>
        }
      </ul>                                                            
    }

     

Please Sign in or register to post replies

Write your reply to:

Draft