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 108 posts 410 karma points
    Dec 05, 2018 @ 10:18
    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 548 posts 1840 karma points MVP 4x c-trib
    Dec 05, 2018 @ 11:08
    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 108 posts 410 karma points
    Dec 05, 2018 @ 11:10
    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 548 posts 1840 karma points MVP 4x c-trib
    Dec 05, 2018 @ 11:35
    Frans de Jong
    0

    I'm sorry, I'm not clear with my explanation.

    Model.Content.AncestorOrSelf(1) is the same as Model.Content.Site()

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

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Dec 05, 2018 @ 11:38
    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 548 posts 1840 karma points MVP 4x c-trib
    Dec 05, 2018 @ 11:39
    Frans de Jong
    0

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

  • André Lange 108 posts 410 karma points
    Dec 05, 2018 @ 11:48
    André Lange
    0

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

  • André Lange 108 posts 410 karma points
    Dec 05, 2018 @ 11:55
    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 548 posts 1840 karma points MVP 4x c-trib
    Dec 05, 2018 @ 15:42
    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 108 posts 410 karma points
    Dec 06, 2018 @ 08:58
    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 548 posts 1840 karma points MVP 4x c-trib
    Dec 06, 2018 @ 09:01
    Frans de Jong
    0

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

  • André Lange 108 posts 410 karma points
    Dec 06, 2018 @ 09:03
    André Lange
    0

    Yep, i figured i needed to find the top page of type TextPage. From then i could use the normal functionality of just using .children to get all the children i need for the menu.

  • André Lange 108 posts 410 karma points
    Dec 06, 2018 @ 09:04
    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 548 posts 1840 karma points MVP 4x c-trib
    Dec 06, 2018 @ 09:08
    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 108 posts 410 karma points
    Dec 06, 2018 @ 09:28
    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 548 posts 1840 karma points MVP 4x c-trib
    Dec 06, 2018 @ 09:46
    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 108 posts 410 karma points
    Dec 06, 2018 @ 09:48
    André Lange
    0

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

  • André Lange 108 posts 410 karma points
    Dec 06, 2018 @ 10:12
    André Lange
    0

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

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Dec 06, 2018 @ 13:15
    Frans de Jong
    0

    No problem, glad I could help!

Please Sign in or register to post replies

Write your reply to:

Draft