Copied to clipboard

Flag this post as spam?

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


  • Jason 92 posts 175 karma points
    Nov 04, 2014 @ 15:34
    Jason
    0

    My attempt at writing a dynamic navigation - help!

    Hello,

    My navigation/node structure is as follows:

    Home
    --About us
    --Our Services
    -----Finance
    -----Insurance

    My current code loops through the nodes and lists them in an unordered list. When the 'CurrentPage' has children, the menu includes the children.

    The problem with this loop is that when one of those children (Finance or Insurance) is the 'CurrentPage', the 'if' condition on 'Our Services' node fails as the CurrentPage no longer has children, therefore 'Finance' and 'Insurance' no longer remain in the menu.

    I want to add to my 'if', an '||' clause which would basically say 'or CurrentPage is a child node'. I'm aware of Model.Content.IsDescendant(), but not sure on how to use it.

    Code below thanks

    <ul>
        @foreach (var page in @CurrentPage.AncestorOrSelf(1).Children)
        {
            <li class="menuItems @(page.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">
                <a href="@page.Url">@page.Name test</a>
            </li>
    
    
            if (@CurrentPage.Children.Count() > 0)
            {                
                foreach (var childPage in page.Children)
                {                    
                    <li class="menuItems menuChildItems @(childPage.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">
                        <a href="@childPage.Url">@childPage.Name</a>
                    </li>
                }
            }
    
        }
    </ul>
    
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 04, 2014 @ 17:01
    Dennis Aaen
    0

    Hi Jason,

    I have tried to understand your structure, and where you want to display the children pages. If I have understand it correct correct you want to display child pages, when you are on a page that has children.

    Try this code and see if it does what you need. if not, then don't hesitate to write again, and would try to help you.

    <ul>
        @foreach (var page in CurrentPage.AncestorOrSelf(1).Children.Where("Visible"))
        {
            <li class="menuItems @(page.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">
                <a href="@page.Url">@page.Name</a>   
            </li>
           
        }
        @if (CurrentPage.Children.Where("Visible").Any()){ 
            <ul>
                @foreach(var childPage in CurrentPage.Children.Where("Visible")){
                    <li class="menuItems menuChildItems @(childPage.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">
                        <a href="@childPage.Url">@childPage.Name</a>
                    </li>
                }
            </ul>
        }
    </ul>

    Hope this helps,

    /Dennis

  • Jason 92 posts 175 karma points
    Nov 04, 2014 @ 17:05
    Jason
    100

    Hi Dennis,

    Thanks for the suggestion, I inserted the code but it generates some interesting menu behaviour.

    I have since found a solution. For anyone else struggling with a similar issue, code is below:

     @foreach (var page in CurrentPage.AncestorOrSelf(1).Children.Where("Visible"))
        {
            <li class="menuItems @(page.Id == CurrentPage.Id ? "selected" : "")">
                <a href="@page.Url">@page.Name</a>
            </li>
    
            if (CurrentPage.Id == page.Id || CurrentPage.Parent.Id == page.Id)
            {
                foreach (var childPage in page.Children)
                {
                    <li class="menuItems menuChildItems @(childPage.Id == CurrentPage.Id ? "selected" : "")">
                        <a href="@childPage.Url">@childPage.Name</a>
                    </li>
                }
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft