External links in navigation with option for opening in a new window
Hi all, I'm a bit stuck. I'm dabbling in Umbraco and would like to have one of my navigation items point to an external link. I have managed to do this by creating a new document type (Linkpage) using this as a template:
<%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true" %> <%@ Import Namespace="umbraco.BusinessLogic" %> <%@ Import Namespace="umbraco.cms.businesslogic.web" %> <%@ Import Namespace="umbraco.presentation.nodeFactory" %> <script runat="server"> void Page_Load(object sender, System.EventArgs e) { // Get the current page Node currentNode = Node.GetCurrent(); // verify that the property "URL" is not null and that it is not empty; then redirect if (currentNode.GetProperty("URL") != null && currentNode.GetProperty("URL").Value.ToString().Trim() != "") { Response.Redirect(currentNode.GetProperty("URL").Value.ToString().Trim()); } } </script> <asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server"> </asp:Content>
However I'd also like the option to make the link open in a new window using a checkbox in the content properties, like this:
As you can see, I've already made a new property called NewTab, but I can't work out how to use this property to influence the link and insert the vital 'target="_blank"' code. Is there a way to do this? My mind (and optimism) tells me it must be possible, but I just don't understand the code I'm working with well enough to figure it out.
I'm working with Umbraco 4.7.0.0, and can work with html and css easily enough, but have little to no experience with xslt or C#.
PS - if there is a better or neater way of doing what I have already achieved, please do suggest it. As I said I don't fully understand all the code I have used and have taken it from other websites/forum posts. Thank you!
(As I see it, the C# code will just make sure that if someone types the angelpants.org/buy-angelpants URL into the addressbar of their browser, they will be redirected to the external link. The XSLT already makes sure to generate the external link correct in navigations.)
The difference is really just an idiomatic thing - they get you to the same place but in different ways - one uses strings, the other uses a very integral part of XSLT - nodesets and axes:
## name() = 'Linkpage' does this:
1. The name() function is called. When no argument is passed, it uses the current context node (current node in the for-each) and returns a string with the name of that element, which actually is a little more complicated than it sounds, because an element in XML has a so-called Qualified Name.
2. The returned string is used in the expression with the 'Linkpage' string, which is then wrapped in a boolean() function (*) to determine if the expression is true() or false().
## self::Linkpage does this:
1. The processor uses the self:: axis to select the Linkpage element (which will of course only return a node if the current node is a <Linkpage> element).
2. The nodeset is wrapped in a boolean() function (*) to return either true() or false().
(*) The result of an XPath in a test attribute will always be wrapped in a boolean() function, to ensure either true() or false() - any nodeset containing atleast one node will return true(), while an empty nodeset returns false().
Goodness, I have a lot to learn. Thank you, that did make things a little clearer, and your earlier advice made my code do exactly what I wanted. Thanks!
External links in navigation with option for opening in a new window
Hi all, I'm a bit stuck. I'm dabbling in Umbraco and would like to have one of my navigation items point to an external link. I have managed to do this by creating a new document type (Linkpage) using this as a template:
...and this as the navigation xslt file:
This works, which is awesome.
However I'd also like the option to make the link open in a new window using a checkbox in the content properties, like this:
As you can see, I've already made a new property called NewTab, but I can't work out how to use this property to influence the link and insert the vital 'target="_blank"' code. Is there a way to do this? My mind (and optimism) tells me it must be possible, but I just don't understand the code I'm working with well enough to figure it out.
I'm working with Umbraco 4.7.0.0, and can work with html and css easily enough, but have little to no experience with xslt or C#.
Please help!
PS - if there is a better or neater way of doing what I have already achieved, please do suggest it. As I said I don't fully understand all the code I have used and have taken it from other websites/forum posts. Thank you!
Hi Sarah,
You shouldn't need the C# stuff at all (unless I'm missing something).
In the XSLT where you check for the LinkPage doctype, you can do this to test the newTab checkbox:
/Chriztian
(As I see it, the C# code will just make sure that if someone types the angelpants.org/buy-angelpants URL into the addressbar of their browser, they will be redirected to the external link. The XSLT already makes sure to generate the external link correct in navigations.)
/Chriztian
Amazing, that worked straight away! I didn't realise I could put the <xsl:attribute...> inside the <xsl:if...> tags.
To help me understand what's going on, how is
different from
(apart from the target=_blank bit, obviously)
Hi Sarah (and welcome to the Umbraco forums!)
The difference is really just an idiomatic thing - they get you to the same place but in different ways - one uses strings, the other uses a very integral part of XSLT - nodesets and axes:
## name() = 'Linkpage' does this:
1. The name() function is called. When no argument is passed, it uses the current context node (current node in the for-each) and returns a string with the name of that element, which actually is a little more complicated than it sounds, because an element in XML has a so-called Qualified Name.
2. The returned string is used in the expression with the 'Linkpage' string, which is then wrapped in a boolean() function (*) to determine if the expression is true() or false().
## self::Linkpage does this:
1. The processor uses the self:: axis to select the Linkpage element (which will of course only return a node if the current node is a <Linkpage> element).
2. The nodeset is wrapped in a boolean() function (*) to return either true() or false().
(*) The result of an XPath in a test attribute will always be wrapped in a boolean() function, to ensure either true() or false() - any nodeset containing atleast one node will return true(), while an empty nodeset returns false().
/Chriztian
Goodness, I have a lot to learn. Thank you, that did make things a little clearer, and your earlier advice made my code do exactly what I wanted. Thanks!
is working on a reply...