Copied to clipboard

Flag this post as spam?

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


  • Craig100 1136 posts 2523 karma points c-trib
    Oct 19, 2011 @ 14:51
    Craig100
    0

    Top Menu active when sub-page selected

    Hi,

    I have a top level menu on a master page. Each top-level page has a side menu. If a side menu page is showing I want the top level menu item to show as active, right down the branch. It works fine for top level pages but I can't get the syntax right for the pages down the branches. Any help would be appreciated as still a newbie working on my first Umbraco site.

    Craig

    Code:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    <ul>
     @{
       string style="";
       }
    
     @if(Model.Name=="Welcome"){style = " class=\"main-active\"";}
    
      <li @Html.Raw(style)>
        <a href="/Welcome" title="Welcome">Welcome</a>
      </li>
    
    @foreach (var page in Model.AncestorOrSelf(1).Children.Where("Visible").Where("showInHeaderMenu"))
    {
    style="";
    
     //-- Switches on top level menu if current page is a top level page
    if([email protected]){style = " class=\"main-active\"";}
    
    //-- Need to switch on top level menu if current page is a descendent of a top level page (showInHeaderMenu)
    //-- I know this next line is rubbish!
    //if(Model.First()=Model.AncestorOrSelf("showInHeaderMenu")){style = " class=\"main-active\"";}
    
      <li @Html.Raw(style)>
        <a href="@page.Url" title="@page.Name">
          @page.Name
        </a>
      </li>  
    }
    </ul>
  • Alex 78 posts 136 karma points
    Oct 19, 2011 @ 15:11
    Alex
    0

    Try replacing you if statement with the below, this will get a DynamicNodeList of the current node and it's ancestors then select the first with the showInHeaderMenu attribute set to true from that list, and compare it to the current "page" (menu item) being rendered. This is untested.

    if (page.Id == Model.AncestorsOrSelf().Where("showInHeaderMenu").FirstOrDefault().Id) { style = " class=\"main-active\""; }
  • Craig100 1136 posts 2523 karma points c-trib
    Oct 19, 2011 @ 15:27
    Craig100
    0

    Thanks Alex, but that gets me the following error:-

    "Cannot perform runtime binding on a null reference"

    Craig

  • Anthony pj 40 posts 63 karma points
    Oct 19, 2011 @ 18:43
    Anthony pj
    1

    Hi craig didnt i give you asolution for soemthing similar the otyher day http://our.umbraco.org/forum/developers/razor/24958-Top-menu-active-from-side-menu

    anywya here again with your new variable

    @foreach (var item in Model.AncestorOrSelf(1).Children.Where("Visible && showInHeaderMenu" )) {
        
          <li class="@Model.IsDescendantOrSelf(item,"main-active", "")" >
            <a href="@item.Url" >@item.Name</a>
          </li>
          }

     

    or if you perfer

    @foreach (var item in Model.AncestorOrSelf(1).Children.Where("Visible && showInHeaderMenu")) {
          var selected = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"main-active;\"" : "";
          <li @Html.Raw(selected)>
            <a href="@item.Url">@item.Name</a>
          </li>
          }

    good luck let me know if missing objective here

  • Craig100 1136 posts 2523 karma points c-trib
    Oct 19, 2011 @ 19:14
    Craig100
    0

    Hi Anthony, thanks for this.

    I get "Incorrect number of parameters supplied for lambda declaration" due to changing

    @foreach (var page in Model.AncestorOrSelf(1).Children.Where("Visible").Where("showInHeaderMenu"))

    to 

    @foreach (var item in Model.AncestorOrSelf(1).Children.Where("Visible && showInHeaderMenu" ))

    However, that aside, it's now working and streamlined, thanks to your contribution. The final code is here: (sorry, no "code" style available!)

    @inherits umbraco.MacroEngines.DynamicNodeContext
    <ul>
     @{
       string style="";
       string active="class=\"main-active\"";
       }

     @if(Model.Name=="Welcome"){style = active;}
      <li @Html.Raw(style)>
        <a href="/Welcome" title="Welcome">Welcome</a>
      </li>

    @foreach (var page in Model.AncestorOrSelf(1).Children.Where("Visible").Where("showInHeaderMenu"))
    {
    style = Array.IndexOf(Model.Path.Split(','), page.Id.ToString()) >= 0 ? active : "";
     <li @Html.Raw(style)>
        <a href="@page.Url" title="@page.Name">
          @page.Name
        </a>
      </li>  
    }
    </ul>

     

    Craig

     

  • Anthony pj 40 posts 63 karma points
    Oct 19, 2011 @ 19:18
    Anthony pj
    0

    Glad you resolved it craig .... what version of umbraco you running i tested this before i sent over ... I am running in 4.7.1

  • Craig100 1136 posts 2523 karma points c-trib
    Oct 19, 2011 @ 19:29
    Craig100
    0

    Also running 4.7.1, via WebMatrix on Win7 in a VirtualBox VM on Ubuntu. Don't let MS touch my iron anymore;)

    Thanks for your help, not sure what to do about marking the answer though as it didn't work for me. However I suppose I can as these comments are with it as an explanation:)

    Craig

Please Sign in or register to post replies

Write your reply to:

Draft