Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sarah 4 posts 24 karma points
    Aug 04, 2012 @ 23:54
    Sarah
    0

    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>

    ...and this as the navigation xslt file:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [
        <!ENTITY nbsp "&#x00A0;">
    ]>
    <xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library"
      exclude-result-prefixes="msxml umbraco.library">


        <xsl:output method="xml" omit-xml-declaration="yes" />

        <xsl:param name="currentPage"/>

        <!-- Input the documenttype you want here -->
        <xsl:variable name="level" select="1"/>

        <xsl:template match="/">
       
     <ul id="topNavigation">
           <li class="home">
             <xsl:if test="$currentPage/@id = $currentPage/ancestor-or-self::* [@level=$level]/@id">
                 <xsl:attribute name="class">home current</xsl:attribute>
             </xsl:if>
             <a href="/">Home</a>
           </li>
          <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
      <li>
         <xsl:if test="@id = $currentPage/@id">
            <xsl:attribute name="class">current</xsl:attribute>
          </xsl:if>
    <xsl:choose>
      <xsl:when test="name() = 'Linkpage'">
        <a href="{current()/uRL}">
          <xsl:value-of select="@nodeName" />
        </a>
      </xsl:when>
      <xsl:otherwise>
        <a href="{umbraco.library:NiceUrl(@id)}">
          <xsl:value-of select="@nodeName" />
        </a>
      </xsl:otherwise>
    </xsl:choose>
      </li>
      
      
    </xsl:for-each>
    </ul>

        </xsl:template>

    </xsl:stylesheet>

    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!

  • Sarah 4 posts 24 karma points
    Aug 04, 2012 @ 23:58
    Sarah
    0

    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!

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 05, 2012 @ 00:04
    Chriztian Steinmeier
    0

    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:

    <xsl:when test="self::Linkpage">
        <a href="{uRL}">
            <xsl:if test="newTab = 1"> <xsl:attribute name="target">_blank</xsl:attribute> </xsl:if>
            <xsl:value-of select="@nodeName" />
        </a>
    </xsl:when>
    

    /Chriztian

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 05, 2012 @ 00:09
    Chriztian Steinmeier
    0

    (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

  • Sarah 4 posts 24 karma points
    Aug 05, 2012 @ 00:12
    Sarah
    0

    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

    <xsl:whentest="self::Linkpage">
           
    <ahref="{uRL}">
                   
    <xsl:iftest="newTab = 1"><xsl:attributename="target">_blank</xsl:attribute></xsl:if>
                   
    <xsl:value-ofselect="@nodeName"/>
           
    </a>
    </xsl:when>

    different from

    <xsl:whentest="name() = 'Linkpage'">
        <ahref="{current()/uRL}">
          <xsl:value-ofselect="@nodeName"/>
        </a>
      </xsl:when>

    (apart from the target=_blank bit, obviously)

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 05, 2012 @ 00:43
    Chriztian Steinmeier
    0

    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

  • Sarah 4 posts 24 karma points
    Aug 05, 2012 @ 01:07
    Sarah
    0

    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!

Please Sign in or register to post replies

Write your reply to:

Draft