Copied to clipboard

Flag this post as spam?

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


  • David Tregoning 63 posts 236 karma points
    Jan 09, 2014 @ 15:58
    David Tregoning
    1

    Checking document type of level 2 node

    I am new to Umbraco and Razor but wanted to know if it is possible that partial views can be coded to check against which document type is used at level 2 of their node tree.

    I wish to create a page (Locations) as per the following structure and planned to write an 'if statement' in the partial view the produces the main navigation that based on the document type of the level 2 node in that node tree

    • Home (homepage)
      • About (textpage)
      • France (homepage)
        • Locations (textpage)

    So any page under France could then only display level 3 pages in its main navigation but using the if statement any page that does not have (a homepage) document type at level 2 can display any level 2 pages* in its main navigation

    *will then 'hide in navigation' the France node

    Is the above possible or am I looking at this with the wrong logic?

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 10, 2014 @ 19:25
    Jeavon Leopold
    0

    Hi David,

    Correct me if I'm wrong but I think you are just talking about showing siblings?

    Perhaps this might work:

    foreach (var page in Model.Content.Siblings())
    {
        <p>@page.Name</p>
    }
    

    Jeavon

  • David Tregoning 63 posts 236 karma points
    Jan 13, 2014 @ 10:24
    David Tregoning
    0

    Hi Jeavon, Thanks for that but I am particually interested in targeting the 2nd level node of the tree the user is in. For example there maybe a page called Paris (textpage) under Locations (textpage) which would need to check the nodealias of France (homepage). I think I have some code that does this:

    var homePage = CurrentPage.AncestorsOrSelf(2).First();
    var menuItems = homePage.Children.Where("UmbracoNaviHide == false");
    @foreach (var page in CurrentPage.AncestorOrSelf(1).Children.Where("Visible")){
        <p>@page.Name</p>
    }
    

    I would then have to add some additional code to check nodealias so see if it is a homepage and if not then the following line would be replaced above

    var homePage = CurrentPage.AncestorsOrSelf(1).First();
    

    This way the About(textpage) would target Home(homepage) as its root where any page under france would target France(homepage) as its home page.

    It seems to be working on my test site, though I am sure there is a cleaner way of writing the code.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 14, 2014 @ 13:54
    Jeavon Leopold
    0

    Hi David,

    Ah ok, so "Paris" and "Home" are both of doc type "homepage", could you do something like this:

    @{
        var homePage = CurrentPage.AncestorOrSelf("HomePage");
        if (homePage != null)
        {
            var menuItems = homePage.Children;
            foreach (var page in menuItems)
            {
                <p>@page.Name</p>
            }
        }
    }
    

    Find the first node of "HomePage" going up the tree?

    Jeavon

  • David Tregoning 63 posts 236 karma points
    Jan 14, 2014 @ 15:15
    David Tregoning
    0

    Thanks again Jeavon,
    I have attacked it from a different angle and seem to have cracked it

    @inherits UmbracoTemplatePage
    @{
    var homePage = CurrentPage.AncestorsOrSelf(1).First();
    var countryHomePage = CurrentPage.AncestorsOrSelf(2).First();
    if (countryHomePage.DocumentTypeAlias == "umbCountryHome"){
        homePage = CurrentPage.AncestorsOrSelf(2).First();
    
    }else{
        homePage = CurrentPage.AncestorsOrSelf(1).First();
    }
    
    var menuItems = homePage.Children.Where("UmbracoNaviHide == false");
    }
    <ul class="mainNav">
    @foreach (var page in menuItems){
        <li>@page.Name</li>
    }
    </ul>
    

    My logic was to get the top level node first (homepage variable) then get the 2nd level node (countryHomePage variable).
    Checked whether countryHomePage has a document Alias of 'umbCountryHome' - this would only be assigned to pages like 'France' then reassign the homepage variable accordingly.
    From there I can assign my menuitems variable and run a loop on this.

    The only thing that stumped me was that I could not initially set the homepage variable to nothing and then reassign it within the if statement - it kept throwing up an error.
    Thanks again

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Jan 14, 2014 @ 15:26
    Jeavon Leopold
    100

    No problem, I think this could be simplified to:

    @{
        var homePage = CurrentPage.AncestorOrSelf(1);
        var countryHomePage = CurrentPage.AncestorOrSelf(2);
        if (countryHomePage.DocumentTypeAlias == "umbCountryHome")
        {
            homePage = CurrentPage.AncestorOrSelf(2);
    
        }
        var menuItems = homePage.Children.Where("Visible");
    }
    <ul class="mainNav">
        @foreach (var page in menuItems)
        {
            <li>@page.Name</li>
        }
    </ul>    
    
  • David Tregoning 63 posts 236 karma points
    Jan 16, 2014 @ 12:11
    David Tregoning
    0

    Perfect, thanks Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft