Copied to clipboard

Flag this post as spam?

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


  • Neil 63 posts 105 karma points
    Nov 20, 2012 @ 19:46
    Neil
    0

    best way put link separators in horizontal list

    I am trying to modify the footerNav.xslt to put vertical separators ('|') only between links.  In C#, you'd set a flag to 0, enter the for-each loop and test the flag to see if it's 1, and if not, put a separator before the link itself before setting the flag to 1 for the rest of the iterations.

    Another way to do this would be to test if the node who's link you're displaying is the first in the list, but I'm totally new to xslt and so need some help to do that or the flag setting I was thinking about above. 

    Any suggestions on the best/easiest way to position these vertical separators using xslt would be greatly appreciated.

  • Jeff 11 posts 73 karma points
    Nov 20, 2012 @ 20:56
    Jeff
    1

    Hi Neil,

    I'll take a stab at it by providing an example. My code loops through pages of the current page and constructs an Unordered HTML list. On each loop through I test to make sure the current page isn't the last page in the list of children. If it is the last page in the list then it simply does not output the separator:

     

    <ul>
    <!-- loop through each child page of the current page. -->
    <!-- explaination of what elements I'm selecting: -->
    <!-- $currentPage/* selects all XML elements under the current node. [@isDoc] targets only elements with the 'isDoc' attribute. This makes sure we're
    selecting only child nodes that are also pages of the website.  -->

    <xsl:for-each select="$currentPage/* [@isDoc]"> 
    <li>
    <!-- use braces or curly brackets '{}' as a short hand of inserting xslt output into attributes of HTML tags: <element attribute="{xslt output}">
    e.g. <a href="{./@id}">-->
    <!-- using a '.' within the for-each loop targets the current xml element the loop is on ./@id selects the id attribute of the current element -->
    <!-- umbraco.library:NiceUrl() see: http://our.umbraco.org/wiki/reference/umbracolibrary/niceurl -->
    <a href="{umbraco.library:NiceUrl(./@id)}">
    <!-- all nodes in umbraco have a nodeName Attribute. I'm simply selecting the @nodeName to display as the text of the link-->
    <xsl:value-of select="@nodeName" />
    </a>
    <!-- To Answer your question: there might be a better way but this is the first thing that came to mind.-->
    <!-- Test to make sure the current element's id (./@id) is NOT equal to the id of the last element in the child nodes of the current node
    ($currentPage/* [@isDoc][last()]/@id)  -->
    <xsl:if test="./@id != $currentPage/* [@isDoc][last()]/@id">
    <!-- Output the character used as your separator.-->
    <xsl:text>|</xsl:text>
    </xsl:if>
    </li>
    </xsl:for-each>
    </ul>

     

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 20, 2012 @ 21:14
    Chriztian Steinmeier
    0

    @Jeff: Two comments:

    1. Inside a for-each (or a match template) you don't need the "./" - the current node is used automatically, which just makes it a wee bit easier to read.

    2. You can perform the test at the end by comparing the current node's position() to the value of last(), like this:

    <xsl:if test="not(position() = last())"> | </xsl:if>

     

    ++Total kudos for explaining so detailed what's going on!

    /Chriztian

  • Neil 63 posts 105 karma points
    Nov 20, 2012 @ 21:23
    Neil
    0

    I tried both and for some reason the first didn't work quite right, but the Chriztian's did.  Thanks so much.  I knew there had to be a way to test for first or last position.  In the default footerNav.xslt that comes with umbraco out of the box, they test for the actual name of the last node, but then that requires that to not change, which is less than ideal.  This works perfectly.

    and I agree with Chriztian, ++kudos for all that amazing detailed explanation for a noob such as myself.

  • Jeff 11 posts 73 karma points
    Nov 20, 2012 @ 21:54
    Jeff
    0

    Ah, that makes sense about ruling out the last position Chriztian. I guess it's just my preference to include the "./". It helps me remember I’m dealing with the current node since I'm explicitly stating it with the '.'.

    I'm glad you got it working Neil. I taught myself xslt over the last few years of working with Umbraco and I found myself returning to these resources time and time again:

    http://our.umbraco.org/wiki/reference/umbracolibrary

     

    http://www.w3schools.com/xpath/ (specifically the syntax, axes, and operators)

    and of course, these forums. What a great community :) 

     

     

  • Neil 63 posts 105 karma points
    Nov 20, 2012 @ 21:59
    Neil
    1

    Thanks, Jeff, I appreciate all the help.  I'm relatively new to everything umbraco, but so far, I think it's great.  Having cut my teeth coding back in the mid to late 90's when modularity was king, I think umbraco is more aligned with that era than the current state of everything being integration, integration and more integtration.  I've read in places that some people don't like umbraco and I just don't get it.   I think it's intuitive, flexible and powerful.  Thanks again for the help and the pointers to more resources.

Please Sign in or register to post replies

Write your reply to:

Draft