What is the best practice to redirect from a parent node to the first child node in xslt?
Instead of using 'umbracoRedirect' to redirect to a specific page, I would like to create a document type of type 'True/False', and if set to true, redirect from that page to the first child node/page. I am hoping to implement this feature in xslt into a sidebar navigation menu.
Do you really want to redirect ie. send the user from the requested url, to another, or would you rather make the root node in a page hierarchy display the content of the first child node instead of its own content?
If you want to redirect, you could use a urlRewriter rule, I frequently do this in sites where i know the first child node is not going to change. With UrlRewriter you can even let the user keep the original url in their browser, and just make Umbraco show the output from another url like the url of the first child.
If the first chilnode is subject to change it's url, eg. if you will frequently put another node as the first child of the root node, you're probably better of making a special template for the root node, and make this template show the contents of the first child using an xslt.
You can also use a RenderTemplate xslt that renders a subnode inside the parent.
Then you need to create a master template with only the RenderTemplate macro inside it. But be aware that you then will get two pages with the same content, and it might affect your site SEO-wise.
<xsl:template match="/"> <xsl:choose> <xsl:when test="string($PageId)!='' and string($TemplateId)!=''"> <xsl:value-of select="umbraco.library:RenderTemplate($PageId, $TemplateId)" disable-output-escaping="yes"/> </xsl:when> <xsl:otherwise> <xsl:text>An error has occured.</xsl:text><br /> <xsl:text>Please contact 3D Design Media to resolve this error.</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:template>
If the user clicks on 'Project 1' (nav menu), it will automatically take them to the 'Item 1' page (the first child node). Is there a simple way to do this in xslt? Examples will be helpful.
The way I would probably handle this is by updating your navigation menu's xslt to link the 'Project 1' menuitem to the 'Item 1' page. No need for a redirect at all.
I don't know what you've got for nav menu code but this should outline the idea at least.
1. Add another property to the docType(s) that should link a child node (the docType for 'Project 1' in your example).
2. Give the new property the following details: Name = Link to first subpage? Alias = linkToSubpage Type = true/false
3. On the 'Project 1' page, select the Link to first subpage field and publish the page
4. Update the Navigation menu's xslt to look for the value of the linkToSubpage property and link to the first subnode if appropriate. See below.
The xslt for navigation normally looks something like this:
[HERE'S A RE-POST, USING CODE HIGHLIGHTING... THANKS, WARREN!]
The way I would probably handle this is by updating your navigation menu's xslt to link the 'Project 1' menuitem to the 'Item 1' page. No need for a redirect at all.
I don't know what you've got for nav menu code but this should outline the idea at least.
1. Add another property to the docType(s) that should link a child node (the docType for 'Project 1' in your example).
2. Give the new property the following details: Name = Link to first subpage? Alias = linkToSubpage Type = true/false
3. On the 'Project 1' page, select the Link to first subpage field and publish the page
4. Update the Navigation menu's xslt to look for the value of the linkToSubpage property and link to the first subnode if appropriate. See below.
The xslt for navigation normally looks something like this:
Redirect to first child node in xslt
What is the best practice to redirect from a parent node to the first child node in xslt?
Instead of using 'umbracoRedirect' to redirect to a specific page, I would like to create a document type of type 'True/False', and if set to true, redirect from that page to the first child node/page. I am hoping to implement this feature in xslt into a sidebar navigation menu.
Thanks.
You can do that in xslt, simply check your properti and redirect, but you need some redirect xslt extension. In umbraco library this function isn't.
Petr
Do you really want to redirect ie. send the user from the requested url, to another, or would you rather make the root node in a page hierarchy display the content of the first child node instead of its own content?
If you want to redirect, you could use a urlRewriter rule, I frequently do this in sites where i know the first child node is not going to change. With UrlRewriter you can even let the user keep the original url in their browser, and just make Umbraco show the output from another url like the url of the first child.
If the first chilnode is subject to change it's url, eg. if you will frequently put another node as the first child of the root node, you're probably better of making a special template for the root node, and make this template show the contents of the first child using an xslt.
Regards
.Hauge
You can also use a RenderTemplate xslt that renders a subnode inside the parent.
Then you need to create a master template with only the RenderTemplate macro inside it.
But be aware that you then will get two pages with the same content, and it might affect your site SEO-wise.
Code:
Template:
Thanks guys.
The first child node is subject to change, so I need a way to always grab the first child node. Let's say my content tree looks like this:
-About Us
-Projects
----Project 1
--------Item 1
--------Item2
----Project 2
-Services
If the user clicks on 'Project 1' (nav menu), it will automatically take them to the 'Item 1' page (the first child node). Is there a simple way to do this in xslt? Examples will be helpful.
Thanks!
You can use xslt function position() to get the first child item. The example below read the 1st child item of the current page.
<xsl:value-of select="$currentPage/node [position()=1]"/>
Hope this will help
The way I would probably handle this is by updating your navigation menu's xslt to link the 'Project 1' menuitem to the 'Item 1' page. No need for a redirect at all.
I don't know what you've got for nav menu code but this should outline the idea at least.
1. Add another property to the docType(s) that should link a child node (the docType for 'Project 1' in your example).
2. Give the new property the following details:
Name = Link to first subpage?
Alias = linkToSubpage
Type = true/false
3. On the 'Project 1' page, select the Link to first subpage field and publish the page
4. Update the Navigation menu's xslt to look for the value of the linkToSubpage property and link to the first subnode if appropriate. See below.
The xslt for navigation normally looks something like this:
<ul id="topnavigation">
<xsl:for-each select="$currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']">
<li>
<a href="{umbraco.library:NiceUrl(@id)}">
<xsl:value-of select="@nodeName"/>
</a>
</li>
</xsl:for-each>
</ul>
I would change it to something like this instead:
<ul id="topnavigation">
<xsl:for-each select="$currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']">
<li>
<a>
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="count(./node) > 1 and string(data[@alias='linkToSubpage']) = '1'">
<xsl:value-of select="umbraco.library:NiceUrl(./node/@id)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="umbraco.library:NiceUrl(@id)" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="@nodeName"/>
</a>
</li>
</xsl:for-each>
</ul>
cheers,
doug.
Nice solution Doug, not sure if you know but the preformatted option from the dropdown does the code highlighting :)
[HERE'S A RE-POST, USING CODE HIGHLIGHTING... THANKS, WARREN!]
The way I would probably handle this is by updating your navigation menu's xslt to link the 'Project 1' menuitem to the 'Item 1' page. No need for a redirect at all.
I don't know what you've got for nav menu code but this should outline the idea at least.
1. Add another property to the docType(s) that should link a child node (the docType for 'Project 1' in your example).
2. Give the new property the following details:
Name = Link to first subpage?
Alias = linkToSubpage
Type = true/false
3. On the 'Project 1' page, select the Link to first subpage field and publish the page
4. Update the Navigation menu's xslt to look for the value of the linkToSubpage property and link to the first subnode if appropriate. See below.
The xslt for navigation normally looks something like this:
I would change it to something like this instead:
cheers,
doug.
Awesome.
Doug, your solution is exactly what I was looking for. Thanks everyone.
Chris
Nice solution. Just what I needed after 2 hours of trying to make something similar :-)
Hi Doug (or anyone else for that matter),
I was trying to get this working with the new XML schema, but can't figure why this isn't working?
Even with the linkToSubpage DocType having a true value, it ignores the child nodes URL and just uses it's current URL.
Any ideas where I'm going wrong?
Many thanks,
Barney
Try the following
Replace:
With:
(note the single Quote's around the last 1)
Hey Gerben, cheers for chipping in but unfortunately I'm getting the same result.
Mmm try the code below. (note the check for greater than 0 instead of greater than 1).
Not sure what you want to achive, but this one checks for ANY child node (instead of more than 1 child node)
Otherwise you might post the source XML snippet so we can have a look at that?
Oh my giddy, that worked! Thanks Gerben!
Hi,
I am new in umbraco world and I try to use this code but it doesn't work.
My content tree looks like this:
- News
- News 1
- News 2
- Products
- Product 1
- Product 2
When I click on News or Product, actually the url is http://localhost/news.aspx but I need to go on this url http://localhost/news/news1.aspx
My code :
In this code count(./*[@isDoc]) always return 0.
Thanks
is working on a reply...