What is the best practice to redirect from a parent node to the nth
child node instead of using 'umbracoRedirect' to redirect to the
specific page? The site structure is setup like this:
homepage
Category 1
Project 1
Gallery 1
SubGallery 1
SubGallery 2
Gallery 2
Gallery 3
SubGallery X
Project #
Gallery #
Category 2
Project A
Gallery A
Project #
Gallery #
Textpage
So, when you are on the homepage and click on Category 1, this actually takes you to SubGallery 1; but if you click on Gallery 2, this would take you right there; or, if you clicked on Gallery 3, this would take you to SubGallery X? I think this would be a type of deeplinking but I've not done that before so I'm probably wrong?
I might have got the wrong idea about what you are trying to do. But if what you want is to redirect to the last leaf node of the current node (ie. the last descendant node in the tree) then this should do it:
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var last = Model.DescendantsOrSelf().Last();
if (last != null && Model.Id != last.Id)
{
Response.Redirect(last.Url);
}
}
I'm probably not quite understanding (not your fault) but it sounds like you want to find the first descendant page that is a Gallery and then, from there, the last descendant Gallery? So, I think you could do it in two steps.
1) var firstGallery = Model.DescendantsOrSelf("Gallery").First();
2) var gallery = firstGallery.DescednantsOrSelf("Gallery").Last();
Thanks to you I also solved the sub-nav issue as well!
First though, the "gallery.DescendantsOrSelf("nodeTypeAlias").First()" was not working it was either throwing an error or just wouldn't return the grandchild at all? So, I went back into the post by Gareth Evans [http://umbraco.com/follow-us/blog-archive/2011/3/13/umbraco-razor-feature-walkthrough-part-5] and did a little refactoring to come up with this, which works:
Hi! I'm not sure why First() doesn't work for you, as I've tried it my self in some simple examples and it seems to work. But I don't know the Umbraco internals, so there may be circumstances (perhaps with nulls or something) that screw it up. But it seems you have a clever work-around using Down(). The only thing to beware of is that (according to docs) "If a node doesn't match the requested node, you'll get null - so look out for null references." So you might want to check that firstGallery isn't null before referencing it.
Redirect to nth child node in Razor?
What is the best practice to redirect from a parent node to the nth child node instead of using 'umbracoRedirect' to redirect to the specific page? The site structure is setup like this:
So, when you are on the homepage and click on Category 1, this actually takes you to SubGallery 1; but if you click on Gallery 2, this would take you right there; or, if you clicked on Gallery 3, this would take you to SubGallery X? I think this would be a type of deeplinking but I've not done that before so I'm probably wrong?
I've already done this with xslt from the link below, but only to the first child and would now like to use Razor and link deeper? [ http://our.umbraco.org/forum/developers/xslt/2834-Redirect-to-first-child-node-in-xslt?p=0 ]
If anyone would like to point me in the right direction I would be eternally grateful!
Here is my code at the moment:
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
dynamic projects = @Model.Descendants("stfuProjectpage").Where("ChildrenAsList.Count > 0").FirstOrDefault();
if(projects != null) {
<nav id="top_nav">
<a class="hidden" href="#main" title="Skip to content">Skip to Content</a>
<ul class="horizontal piped">
@foreach (var gallery in projects) {
var selected = Array.IndexOf(@Model.Path.Split(','), @gallery.Id.ToString()) >= 0 ? "navigation selected" : "navigation";
<li><a class="@selected" href="@gallery.Url">@gallery.Name</a></li>
}
</ul>
</nav>
}
}
Thanks in advance!
Forgot to add the code for the sub-nav which also need to be able to jump to the nth child:
Sorry if this confuses the issue any further!
I might have got the wrong idea about what you are trying to do. But if what you want is to redirect to the last leaf node of the current node (ie. the last descendant node in the tree) then this should do it:
Dan,
First thanks for this, I didn't know about the "last()"!
I've now tried this and this takes me too deep: to the first image of the last gallery...
Second, upon closer examination of the initial post I realised I don't think I explained as best I could! Doh!
Let me 'splain again:
User Journey:
Maybe this will help understand what's supposed to happen better!
*Note to self: Don't post late at night anymore!
If you would like to see something more concrete, let me know and I'll send you a link to my development server.
Cheers for any and all help with this!
If you just want to jump to the last gallery then you should be able to use this (assuming your gallery alias is Gallery):
var last = Model.DescendantsOrSelf("Gallery").Last();
Cheers for this one too Dan, sadly it's not the last gallery but the first?
I was able to get the top navigation to work, which is done currently in XSLT, by using
./*/*[@isDoc]"
As you can see in the code snippet below:
But as the homepage thumbnail gallery is created with Razor I need to place this same functionality in there:
Thanks for all your help by the way!
Jon
I'm probably not quite understanding (not your fault) but it sounds like you want to find the first descendant page that is a Gallery and then, from there, the last descendant Gallery? So, I think you could do it in two steps.
1) var firstGallery = Model.DescendantsOrSelf("Gallery").First();
2) var gallery = firstGallery.DescednantsOrSelf("Gallery").Last();
It's hard to test, but those are my thoughts!
DAN!!!!!
We're so bloody close I can smell the coffee!!!!!
Here's my adjustments based on your recommendations:
WHICH WORKS!!!!!!!!
YAYYYYYYY!!!!!
Thank you thank you thank you thank you thank you!
J
Excellent! Very pleased to be of some assistance. Good luck with the rest of your project!
Dan,
Thanks to you I also solved the sub-nav issue as well!
First though, the "gallery.DescendantsOrSelf("nodeTypeAlias").First()" was not working it was either throwing an error or just wouldn't return the grandchild at all? So, I went back into the post by Gareth Evans [http://umbraco.com/follow-us/blog-archive/2011/3/13/umbraco-razor-feature-walkthrough-part-5] and did a little refactoring to come up with this, which works:
Do you see anything wrong with this or a better way to go to the first grandchild of the project?
Thanks for all your help with this and I hope the karma comes right back for ya, let me know if I can return the favour!
J
Hi! I'm not sure why First() doesn't work for you, as I've tried it my self in some simple examples and it seems to work. But I don't know the Umbraco internals, so there may be circumstances (perhaps with nulls or something) that screw it up. But it seems you have a clever work-around using Down(). The only thing to beware of is that (according to docs) "If a node doesn't match the requested node, you'll get null - so look out for null references." So you might want to check that firstGallery isn't null before referencing it.
Anyway, glad to have been of some small help!
is working on a reply...