Copied to clipboard

Flag this post as spam?

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


  • Dan 12 posts 91 karma points
    Sep 01, 2014 @ 15:44
    Dan
    0

    3 level navigation

    Hi there,

    I'm new to Umbraco - and Razor. I'm having trouble creating a 3rd level dropdown nav. My Navigation partial looks like this...

        <nav class="primary-navigation">
    <ul class="list-unstyled list-inline site-links">
    @foreach (var page in root.Children.Where("Visible"))
    {
    <li class="@(page.IsAncestorOrSelf(CurrentPage) ? "active" : null)">
    <a href="@page.Url">@page.Name</a>
    @if (page.Children.Where("Visible").Count() > 0) {
    <div class="menu-dropdown">
    <div class="container">
    <div class="row">

    <div class="col-md-4">
    @foreach (var subpage in page.Children.Where("Visible")){
    <h4 class="list-title">@subpage.Name</h4>
    <!-- THIRD LEVEL LINKS? -->
    }

    </div>


    </div>
    </div>
    </div>
    }
    </li>
    }
    </ul>
    </nav>

    I'm just not sure how to pull out the third level links - and if in fact I'm going about this the best way?

    Any help would be massively appreciated :-)

    I'm using Umbraco 7. 

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 01, 2014 @ 17:16
    Jeavon Leopold
    0

    Hi Dan and welcome to Our!

    You could just create yet another foreach loop with subpage, e.g. subsubpage, but it might make sense to use a Razor Helper method instead. e.g. something based on this:

    @inherits UmbracoTemplatePage
    <nav id="nav" class="skel-panels-fixed">
        <ul>
            @{
                var maxLevelForNav = 4;            
                var home = CurrentPage.AncestorOrSelf(1);
    
                @RenderNavForNode(home, 1)
    
                foreach (var page in home.Children.Where("Visible"))
                {
                    @RenderNavForNode(page, maxLevelForNav);
                }
            }
        </ul>
    </nav>
    
    @helper RenderNavForNode(dynamic page, int maxLevel)
    {
        var current = CurrentPage.Id == page.Id ? "active" : null;
        <li><a class="@current" href="@page.Url">@page.Name</a></li>
        if (page.Children.Where("Visible").Any() && page.Level < maxLevel)
        {
            <ul>
                @foreach (var subPage in page.Children.Where("Visible"))
                {
                    @RenderNavForNode(subPage, maxLevel)
                }
            </ul>
        }
    }
    

    Jeavon

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 01, 2014 @ 17:18
    Jeavon Leopold
    0

    edited above to add maxLevel constraint

  • Dan 12 posts 91 karma points
    Sep 01, 2014 @ 18:29
    Dan
    0

    Hey Jeavon,

    Thanks so much for the response! 

    How would I go about adding another foreach loop? What I've tried below doesn't quite work

    <div class="col-md-4">

      @foreach (var subpage in page.Children.Where("Visible")){

        <h4 class="list-title">@subpage.Name</h4>

         foreach( var childPage in page.Children.Where("Visible")){

          <!-- Links Here --> 

         }   

      }

    </div>

     

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 01, 2014 @ 18:34
    Jeavon Leopold
    101

    I think only that the second one needs to look at Children of subpage:

    <div class="col-md-4">
        @foreach (var subpage in page.Children.Where("Visible"))
        {
            <h4 class="list-title">@subpage.Name</h4>
            foreach (var childPage in subpage.Children.Where("Visible"))
            {
          <!-- Links Here -->
            }
        }
    </div>
    
Please Sign in or register to post replies

Write your reply to:

Draft