Copied to clipboard

Flag this post as spam?

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


  • MB 273 posts 936 karma points
    Aug 08, 2016 @ 08:18
    MB
    0

    Multiple navigations

    Hello everyone,

    I'm looking for the best practice when having two navigations.

    I'll be having a top navigation and a main navigation but I'm wondering about the best setup and how to programmatically achieve this.

    I usually have this setup: enter image description here

    But how do I seperate a menu from another menu? The code I use is this one:

    @{ var selection = CurrentPage.Site(); }
    @Traverse(selection)
    
    @helper Traverse(dynamic node)
    {
    var maxLevelForMenu = 3;
    var selection = node.Children.Where("Visible").Where("Level <=" + maxLevelForMenu);
    
    if (selection.Any())
    {
            <ul>
                @foreach (var page in selection)
                {
                    string currentState = @page.Id != CurrentPage.Id ? "" : "current";
                    string isParent = @page.Children.Where("visible").Count() > 0 ? "parent" : "";
    
                    <li class="@currentState @isParent">
                        <a href="@page.Url"><span>@page.Name</span></a>
                        @Traverse(page)
                    </li>
                }
            </ul>
    }
    }
    
  • David Peck 690 posts 1896 karma points c-trib
    Aug 08, 2016 @ 09:16
    David Peck
    0

    I'm not sure I understand the question. You can have two separate partials one, for each of your menu types. Probably you want these to be cached using @Html.CachedPartial()

    If you want them to display different things, then you need to change the razor as per the requirements of each. In your example you're rendering everything up to max level of 3. What do you want the other one to do?

  • MB 273 posts 936 karma points
    Aug 08, 2016 @ 09:45
    MB
    0

    Hey David,

    Thank you for you're reponse.

    I'll try and approach it from a different angle with some pictures:

    I have a top navigation and a middle navigation: enter image description here

    And inside my Umbraco CMS I have this tree: enter image description here

    I'm wondering where I should make my middle navigation.

    • Should I make it inside the "Website/master" content tree or outside it?
    • If I make 2 partials, one for each navigation, how do I define the starting point/page for each navigation?

    I hope it makes more sense now David

  • David Peck 690 posts 1896 karma points c-trib
    Aug 08, 2016 @ 10:03
    David Peck
    100

    Hi Mike,

    Yes it makes a little bit more sense.

    If my menu is 'built' by the CMS editor rather than being based purely on the content in the tree, then I'll create a 'Site Content' item at the the same level as Website but you could equally just add your menu data to the Website item.

    To design your top menu, I generally use Nested Content or Archetype. This will allow you to design any menu you like as you create the fields.

    I'm assuming that your middle menu changes depending on where in the site you are. If that is designed by the CMS editor, then you could add your menu designer property to every page. When a page it loaded it looks at itself and then every ancestor to find a menu that has been designed e.g.:

    Model.AncestorsOrSelf()
                .Select(x => x.GetPropertyValue<IEnumerable<IPublishedContent>>())
                .Where( x => x != null && x.Count() > 0)
                .FirstOrDefault();
    

    Alternatively if you're middle menu is based on the content tree you could just go to Level 2 (for example) and then list all children e.g.

    @foreach(var menuPage in Model.Content.AncestorOrSelf(2).Children){
        <a href='@menuPage.Url'>@menuPage.Name</a>
    }
    
  • MB 273 posts 936 karma points
    Aug 24, 2016 @ 18:10
    MB
    0

    [edit]

    The last alternative solution you provided worked like a charm! Thank you David! I just changed the AncestorOrSelf number ^_^!

Please Sign in or register to post replies

Write your reply to:

Draft