Copied to clipboard

Flag this post as spam?

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


  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    May 28, 2019 @ 17:05
    Chriztian Steinmeier
    0

    MegaNav: Getting context while rendering navigation

    Hi all,

    I consider myself an expert in rendering navigations using XSLT, and using Razor I've come quite far as well - as long as they're based on the Content structure it's no problem.

    Now I have a site which is using Cogworks' MegaNav package for the navigation, and here's where I get stuck:

    I need to add a class on a top level item if the currently rendered page is somewhere below it. Easy when using the content structure, but when using MegaNav, I don't have access to any Ancestor/Descendant helper methods that works within the structure built by the editor. At least not as far as I can tell...

    How do you handle this scenario (if at all)?

    /Chriztian

  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    May 30, 2019 @ 12:46
    Søren Kottal
    0

    Hi Chriztian

    I have not used MegaNav myself, but it seems like the MeganavItem class has a Content property, where the node of the item is stored (if not external).

    So assuming you do something like this:

    <ul>
      @foreach (var item in Model.Content.GetPropertyValue<IEnumerable<MeganavItem>("mainNavigation")))
    {
    <li>
    ...
    </li>
    }
    </ul>
    

    You should be able to test for ancestors like (Model is the current page rendered)

    if (item.Content != null && item.Content.IsAncestorOf(Model))
    {
       // something to do if current page is in the path of item
    }
    
  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    May 30, 2019 @ 12:56
    Chriztian Steinmeier
    0

    Brilliant, Søren — had forgot about the Is* helpers, actually.

    Will try it out right away.

    /Chriztian

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    May 30, 2019 @ 13:21
    Chriztian Steinmeier
    0

    Ah, but no :(

    I forgot the original problem again: Because the Meganav structure can be totally different from the actual content structure, I really can't use IsAncestorOf() etc.

    E.g.:

    Even with a simple content structure like this:

    Home
      - About
      - Products
    

    I can create a Meganav structure that looks like this:

    Our company
      - Who we are (link to /about/)
    Our products
      - All of them (link to /products/)
    

    When rendering the Products page, I need to set a .currentpath class (or similar) on the Our products item and I can't get that info without traversing the actual Meganav model...

    /Chriztian

  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    May 30, 2019 @ 13:30
    Søren Kottal
    100

    How about this?

    if (item.Children.SelectRecursive(x => x.Children).Select(x => x.Id).Contains(Model.Id)
    

    This one gets all descendants id's of item, and then checks if the current node's id (Model.Id) is one of them.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    May 30, 2019 @ 13:41
    Chriztian Steinmeier
    0

    It's works!

    Never heard of SelectRecursive() before - but it's perfect here :)

    Thanks a ton!

    (Thankfully, the Meganav allows setting a max. level, so I don't think there will be any performance concerns with this.)

    /Chriztian

  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    May 30, 2019 @ 18:39
    Søren Kottal
    0

    Haha, I never used it before - but Intellisense is your friend :)

Please Sign in or register to post replies

Write your reply to:

Draft