Copied to clipboard

Flag this post as spam?

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


  • Johan 95 posts 264 karma points
    Dec 03, 2015 @ 12:26
    Johan
    0

    Hide navigation if empty

    I would like to hide my side navigation if contains no content. I'm sure this can be solved using "if Any something..." Im not quite sure. I would appriciate if you guys could help me out.

    Here is my side navigation code.

    @inherits UmbracoTemplatePage
    
    <div id="LeftMenuHolder">               @*<----Not show these if list is empty*@
        <span class="Head-Round-Red">       @*<----Not show these if list is empty*@
            <span>@CurrentPage.Name</span>  @*<----Not show these if list is empty*@
        </span>@*<----Not show these if list is empty*@
    
        <ul>
            @if (CurrentPage.Parent.DocumentTypeAlias == "Home")
            {
                foreach (var item in CurrentPage.Children.Where("Visible"))
                {
                    <li>
                        <a href="@item.Url">
                            <span>@item.Name</span>
                        </a>
                    </li>
                }
            }
            else
            {
                foreach (var sibling in CurrentPage.Parent.Children)
                {
                    <li>
                        <a href="@sibling.Url">
                            <span>@sibling.Name</span>
                        </a>
                    </li>
                }
            }
        </ul>
        <span class="BoxBot">      @*<----Not show these if list is empty*@
            <span></span>          @*<----Not show these if list is empty*@
        </span>                    @*<----Not show these if list is empty*@
        </div>
    
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 03, 2015 @ 13:10
    Dave Woestenborghs
    100

    I think you can do it like this :

    @inherits UmbracoTemplatePage
    @{
        var navItems = CurrentPage.Parent.DocumentTypeAlias == "Home" ? CurrentPage.Children.Where("Visible").ToList() : CurrentPage.Parent.Children.ToList();
    }
    @if(navItems.Any())
    {
    <div id="LeftMenuHolder">               @*<----Not show these if list is empty*@
        <span class="Head-Round-Red">       @*<----Not show these if list is empty*@
            <span>@CurrentPage.Name</span>  @*<----Not show these if list is empty*@
        </span>@*<----Not show these if list is empty*@
    
        <ul>
            @foreach (var item in navItems)
                {
                    <li>
                        <a href="@item.Url">
                            <span>@item.Name</span>
                        </a>
                    </li>
                }        
        </ul>
        <span class="BoxBot">      @*<----Not show these if list is empty*@
            <span></span>          @*<----Not show these if list is empty*@
        </span>                    @*<----Not show these if list is empty*@
        </div>
    }
    

    This has not been tested but I think it should work.

    Dave

  • Johan 95 posts 264 karma points
    Dec 03, 2015 @ 13:23
    Johan
    0

    Thank you it works

Please Sign in or register to post replies

Write your reply to:

Draft