Copied to clipboard

Flag this post as spam?

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


  • Stephen Davidson 216 posts 392 karma points
    Aug 24, 2015 @ 16:10
    Stephen Davidson
    0

    Display Parent items if no child items in page menu

    I'm building a Section Menu which is nearly there. However when I get to the bottom level or a level where there are no children i want to display all the immediate parents items. So far I have

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    
    @* 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="portfolio-categ filter">      
        @* For each child page under the root node, where the property umbracoNaviHide is not True *@
        @foreach (var childPage in CurrentPage.AncestorOrSelf(2).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 *@
        <span 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>
            }
    
        </span>
    }else{<span> <p>hello</p> </span>}
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft