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?
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.
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
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
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?
Hi David,
Correct me if I'm wrong but I think you are just talking about showing siblings?
Perhaps this might work:
Jeavon
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:
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
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.
Hi David,
Ah ok, so "Paris" and "Home" are both of doc type "homepage", could you do something like this:
Find the first node of "HomePage" going up the tree?
Jeavon
Thanks again Jeavon,
I have attacked it from a different angle and seem to have cracked it
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
No problem, I think this could be simplified to:
Perfect, thanks Jeavon
is working on a reply...