Hi Guys I'm trying to build my first website with only razor, Thanks for the applause :D
I've build a Navigation with subnavigation. I've used multiple if statements to check if my parent node i the one selected/current Page and made an if to check for childrens.
But is there a better way to check for both statement in one if statement? I really cant figurer it out myself :D
@if (Model.Content.Ancestors().Any()) { <ul> @* For each page in the ancestors collection which have been ordered by Level (so we start with the highest top node first) *@ @foreach (var page in Model.Content.AncestorOrSelf(1).Children().Where("UmbracoNaviHide != True")) { <li><a class="@(page.IsAncestorOrSelf(Model.Content) ? "current" : null)" href="@page.Url">@page.Name</a> »</li> if(Model.Content.Id == page.Id) { if (@page.Children().Where("TemplateId !=0 && UmbracoNaviHide != True").Any()) { <ul> @foreach (var subPage in @page.Children().Where("TemplateId !=0 && UmbracoNaviHide != True")) { <li><a href="@subPage.Url">@subPage.Name</a></li> } </ul> } } } @* Display the current page as the last item in the list *@ <li>@CurrentPage.Name</li> </ul> }
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@if (Model.Content.Ancestors().Any())
{
<ul>
@* For each page in the ancestors collection which have been ordered by Level (so we start with the highest top node first) *@
@foreach (var page in Model.Content.AncestorOrSelf(1).Children().Where("UmbracoNaviHide != True"))
{
<li><a class="@(page.IsAncestorOrSelf(Model.Content) ? "current" : null)" href="@page.Url">@page.Name</a> »</li>
if((Model.Content.Id == page.Id) && (page.Children().Where("TemplateId !=0 && UmbracoNaviHide != True").Any()))
{
<ul>
@foreach (var subPage in @page.Children().Where("TemplateId !=0 && UmbracoNaviHide != True"))
{
<li><a href="@subPage.Url">@subPage.Name</a></li>
}
</ul>
}
}
@* Display the current page as the last item in the list *@
<li>@CurrentPage.Name</li>
</ul>
}
Hi Steve I have not tried your suggestion but it was some kind that solution i was looking for with the && i just couldn't find the right way to do it i'll text you back when i try this thanks for your time :D
Ah in that case then I think where you were going wrong was probably not putting your conditions in brackets. You need
if ( (CONDITION1) && (CONDITION2) )
so that razor evaluates each condition and then ADDs it. Hope that makes sense. I suspect the reason that no-one replied is a combination of the big Umbraco Conference and them not being sure what it was you were trying to achieve as I don't quite understand what that menu is trying to output but I hope it helps :)
Hi Steve It was exactly the part with the brackets i missed :D
Well i'm pretty new to razor and just trying du make a navigation at 1 level and i want to display subpages if the active page have chldrens :D
Maybe there is a better way to do it but as i said i'm newbie - and, well i'm actually understanding my code i posted - thats for this moment the most importent thing for me :D
But you are welcome to put on a better solution :D
Do you mean your menu is empty when you're on the page Subpage 02... ? Should it always show all parent pages and only show sub links if you're in that "section" (e.g. either on the parent "Page" or in subpage01 or subpage 02?
Can you post a screen shot of your content tree so we can see your structure and confirm what you should see, when!
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
// First let's get the top most node and then the direct parent from the current page
var homepage = CurrentPage.AncestorsOrSelf(1).First();
var parent = CurrentPage.AncestorsOrSelf(2).First();
@* comment me out - I'm for debugging purposes *@
<p>@homepage.id - @homepage.name</p>
<p>@parent.id - @parent.name</p>
@* then output the level 1 parent nodes (sections) *@
<ul>
@foreach (var parentSec in @homepage.Children().Where("UmbracoNaviHide != True"))
{
<li><a href="@parentSec.Url">@parentSec.Name</a></li>
@* check if we're in that parent section *@
if (@parentSec.id == @parent.id)
{
@* @:FOUND @parentSec.id - @parentSec.name *@
@* we are in the parent section so list out children *@
<ul>
@foreach (var subPage in @parentSec.Children().Where("UmbracoNaviHide != True"))
{
if(subPage.id == CurrentPage.id)
{
<li>@subPage.Name</li>
}
else
{
<li><a href="@subPage.Url">@subPage.Name</a></li>
}
}
</ul>
}
}
</ul>
}
There is no template type checking (if you need this I hope you can add this back in yourself) and it doesn't have your rule about adding the current page at the end of the list (if you want this simply change the output of the conditional that checks the ID against it to not output in the loop and just "hardcode" an output of the current page in that subloop. Hope that all makes sense (and works!).
Razor Multiple if statements
Hi Guys
I'm trying to build my first website with only razor, Thanks for the applause :D
I've build a Navigation with subnavigation. I've used multiple if statements to check if my parent node i the one selected/current Page and made an if to check for childrens.
But is there a better way to check for both statement in one if statement? I really cant figurer it out myself :D
Can no one help me out with this?
Is this all you need?
Hi Steve
I have not tried your suggestion but it was some kind that solution i was looking for with the && i just couldn't find the right way to do it i'll text you back when i try this thanks for your time :D
Ah in that case then I think where you were going wrong was probably not putting your conditions in brackets. You need
if ( (CONDITION1) && (CONDITION2) )
so that razor evaluates each condition and then ADDs it. Hope that makes sense. I suspect the reason that no-one replied is a combination of the big Umbraco Conference and them not being sure what it was you were trying to achieve as I don't quite understand what that menu is trying to output but I hope it helps :)
Hi Steve
It was exactly the part with the brackets i missed :D
Well i'm pretty new to razor and just trying du make a navigation at 1 level and i want to display subpages if the active page have chldrens :D
Maybe there is a better way to do it but as i said i'm newbie - and, well i'm actually understanding my code i posted - thats for this moment the most importent thing for me :D
But you are welcome to put on a better solution :D
Well Steve
I hope you can help me out with the last part.
Frontpage
Page
- Subpage 01
- Subpage 02 (Current page)
My problem is that my menu closes when i select "Subpage 02".
I worksfine when i select "Page"?!?
What wrong with it?
Do you mean your menu is empty when you're on the page Subpage 02... ? Should it always show all parent pages and only show sub links if you're in that "section" (e.g. either on the parent "Page" or in subpage01 or subpage 02?
Can you post a screen shot of your content tree so we can see your structure and confirm what you should see, when!
Hi Steve
It's spot on the problem :D
Any suggestions of what im doing wrong?
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@* Check the current page has ancestors *@
@if (Model.Content.Ancestors().Any())
{
<ul>
@* For each page in the ancestors collection which have been ordered by Level (so we start with the highest top node first) *@
@foreach (var page in Model.Content.AncestorOrSelf(1).Children().Where("UmbracoNaviHide != True"))
{
<li><a class="@(Model.Content.IsDescendantOrSelf(page) ? "current" : null)" href="@page.Url">@page.Name @page.Id</a></li>
if ((Model.Content.AncestorOrSelf().Id == page.Id ) && (page.Children().Where("TemplateId !=0 && UmbracoNaviHide != True").Any()))
{
<ul>
@foreach (var subPage in @page.Children().Where("TemplateId !=0 && UmbracoNaviHide != True"))
{
<li><a href="@subPage.Url">@subPage.Name @subPage.Id</a></li>
}
</ul>
}
}
@* Display the current page as the last item in the list *@
<li>@CurrentPage.Name @Model.Content.Id</li>
</ul>
}
Hi Mads,
Try this code as a starter
There is no template type checking (if you need this I hope you can add this back in yourself) and it doesn't have your rule about adding the current page at the end of the list (if you want this simply change the output of the conditional that checks the ID against it to not output in the loop and just "hardcode" an output of the current page in that subloop. Hope that all makes sense (and works!).
Hi Steve
This is perfect - thank you so much :D
is working on a reply...