Copied to clipboard

Flag this post as spam?

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


  • Natasha 79 posts 195 karma points
    Sep 04, 2015 @ 22:29
    Natasha
    0

    Help with Left Navigation

    Hi there,

    I am new to razor and I need to amend this code. My menu structure is:

    about ==about 1 ===about 1.1 ==about 2 ==about 3

    currently when I'm on about it displays the children and grandchildren, but I want it to show the same menu if i'm on about 1,2,3 or about 1.1 which is level 4 as well.

    This is the code adapted from show descendants of current page: @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @* Ensure that the Current Page has children, where the property umbracoNaviHide is not True @ @if (CurrentPage.Children.Where("Visible").Any()) { @ Get the first page in the children, where the property umbracoNaviHide is not True *@ var naviLevel = CurrentPage.Children.Where("Visible").First().Level;

    @* Add in level for a CSS hook *@
    <ul class="level-@naviLevel">            
        @* For each child page under the root node, where the property umbracoNaviHide is not True *@
        @foreach (var childPage in CurrentPage.AncestorOrSelf(1).Children.Where("Visible"))
        {
            <li>
                <a href="@childPage.Url">@childPage.Name</a>
    
                @* if the current page has any children, where the property umbracoNaviHide is not True *@
                @if (childPage.Children.Where("Visible").Any())
                {                    
                    @* Call our helper to display the children *@
                    @childPages(childPage.Children)
                }
            </li>
        }
    </ul>
    

    }

    @helper childPages(dynamic pages) { @* Ensure that we have a collection of pages @ if (pages.Any()) { @ Get the first page in pages and get the level *@ var naviLevel = pages.First().Level;

        @* Add in level for a CSS hook *@
        <ul class="level-@(naviLevel)">
            @foreach (var page in pages.Where("Visible"))
            {
                <li>
                    <a href="@page.Url">@page.Name</a>
    
                    @* if the current page has any children, where the property umbracoNaviHide is not True *@
                    @if (page.Children.Where("Visible").Any())
                    {                        
                        @* Call our helper to display the children *@
                        @childPages(page.Children)
                    }
                </li>
            }
        </ul>
    }
    

    }

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 05, 2015 @ 08:18
    Dave Woestenborghs
    101

    The problem with this razor code is the first if statement.

    if (CurrentPage.Children.Where("Visible").Any())
    

    If your current page doesn't have any children this won't display the menu.

    I think you can solve it by determing the start item first.

    var startitem = CurrentPage.AncestorOrSelf(1)
    

    Then update the if check

     if (startitem.Children.Where("Visible").Any())
    

    And the determining of the level :

    var naviLevel = startitem.Children.Where("Visible").First().Level;
    

    And ofcourse your for each statement

    foreach (var childPage in startitem.Children.Where("Visible"))
    

    Dave

  • Natasha 79 posts 195 karma points
    Sep 07, 2015 @ 10:14
    Natasha
    0

    Thank you Dave I got it working

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Sep 07, 2015 @ 10:42
    Dave Woestenborghs
    0

    Glad to see you got it working. Don't forget to mark the answer as a solution so others can find it as well.

    Dave

  • Bas Schouten 135 posts 233 karma points
    Nov 25, 2015 @ 13:19
    Bas Schouten
    0

    Is it posible to activate the helper only when the upper level is active?

    I'am new to razor and want to use it as Subnavigation.

  • Michael Nielsen 155 posts 812 karma points
    Nov 25, 2015 @ 14:03
    Michael Nielsen
    0
    var startitem = CurrentPage.AncestorOrSelf(2)
    

    You can set your startitem to any level you wish, and take children from that.

  • Bas Schouten 135 posts 233 karma points
    Nov 25, 2015 @ 14:22
    Bas Schouten
    0

    I Michael,

    Thanks for your reply. I mean that u only see chlidren from active parents.

    My structure in Umbraco is like:

    enter image description here

    At the front I only want to show Subitem 1.1 and Subitem 1.2 when Stubitem 1 is active.

    When subitem 1 is active it doesn't have to show Subitems 2.1 and 2.2

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies