Copied to clipboard

Flag this post as spam?

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


  • André Lange 70 posts 242 karma points
    6 days ago
    André Lange
    0

    Persistant menu on textpage

    Hi, i have a textpage, which can have it self as children.

    In that regard i am trying to figure out a way to have a menu on the page, which persists with the first "textpage", and the children.

    Right now it just changes to show the children from the current node.

    I am going to be able to have up to three levels in this.

     - Home
          - TextPage <-- First level
               - TextPage <-- second level
               - TextPage
                    - TextPage <-- third level
                    - TextPage
    

    How do i persist the menu from the perspective of the first level, when i navigate down the levels.

  • Frans de Jong 406 posts 1344 karma points c-trib
    6 days ago
    Frans de Jong
    0

    You can go up from the current page to the level of choice.

    Model.Content.Site() -- Rootnode of the current page
    Model.Content.AncestorOrSelf(2) -- Node on the second level from the rootnod
    

    So you can do something like:

    var root = Model.Content.Site();
    foreach(var child in root.Children){
    

    }

  • André Lange 70 posts 242 karma points
    6 days ago
    André Lange
    0

    I thought about that as well, but that would mean it could only be a for example level 2.

    What if the first textpage was somehow at level 3 ? then it would miss a step

  • Frans de Jong 406 posts 1344 karma points c-trib
    6 days ago
    Frans de Jong
    0

    I'm sorry for not being clear.

    Model.Content.AncestorOrSelf(1) is Model.Content.Site()

    So if you use Model.Content.AncestorOrSelf(2) it will start at your First level

  • Frans de Jong 406 posts 1344 karma points c-trib
    6 days ago
    Frans de Jong
    0

    Do you have a code example of what you have right now?

  • André Lange 70 posts 242 karma points
    6 days ago
    André Lange
    0

    Right now i just use Currentpage.Children and then i map those to what i need.

  • André Lange 70 posts 242 karma points
    6 days ago
    André Lange
    100

    I think i found a solution.

    var topTextPage = currentPage;
                while (topTextPage.Parent is TextPage tp)
                    topTextPage = tp;
    

    This should go up the tree until it meets something other than a textpage.

  • Frans de Jong 406 posts 1344 karma points c-trib
    6 days ago
    Frans de Jong
    0

    Than I misunderstood you. I thought you wanted a menu showing the siblings but this looks more like a breadcrumb

  • André Lange 70 posts 242 karma points
    5 days ago
    André Lange
    0

    I wanted a persistent menu, which showed children from the point of the first parent that is a textpage.

  • Frans de Jong 406 posts 1344 karma points c-trib
    5 days ago
    Frans de Jong
    0

    What you will get with your code is the ancestors of the page you are on.

  • André Lange 70 posts 242 karma points
    5 days ago
    André Lange
    0

    Yep, i figured i needed to find the parent, even when i am on the third level. So i just look for the top parent which is of type TextPage. And from there i could just use .children.

    I don't know if this is the best solution, but so far it works. Although it is a lot of looking up the tree every page load.

  • Frans de Jong 406 posts 1344 karma points c-trib
    5 days ago
    Frans de Jong
    0

    I don't understand why you don't use AncestorsOrSelf(2). In your example the highest level textpage is on the second level so there's no need to loop through all the parents until you find the highest one right? I would never go with a loop if you know where to go.

    Or is the structure in your first post incorrect?

  • André Lange 70 posts 242 karma points
    5 days ago
    André Lange
    0

    The structure is correct, but it is also possble that the textpage first starts on the 3rd level or lower.

    So it isn't always on the second level.

  • Frans de Jong 406 posts 1344 karma points c-trib
    5 days ago
    Frans de Jong
    0

    If that's the case you can use this

    With modelsbuilder:
        var topLevelTextPage = Model.Content.AncestorsOrSelf<TextPage>().Last();
    
    Without modelsbuilder
            var topLevelTextPage = Model.Content.AncestorsOrSelf("textPage").Last();
    
  • André Lange 70 posts 242 karma points
    5 days ago
    André Lange
    0

    Looks a lot more clean, will try it out later.

  • André Lange 70 posts 242 karma points
    5 days ago
    André Lange
    0

    This works great, alot more clean and easy to understand. Thanks for the help ^^

  • Frans de Jong 406 posts 1344 karma points c-trib
    5 days ago
    Frans de Jong
    0

    No problem, glad I could help!

Please Sign in or register to post replies

Write your reply to:

Draft