Copied to clipboard

Flag this post as spam?

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


  • Rich Green 2246 posts 4008 karma points
    Aug 12, 2009 @ 10:41
    Rich Green
    0

    Setting current page class to "Selected"

    I'm pretty sure this is very basic but I can't seem to find a working example.

    Here's our one level menu

     

    <ul class="links">
    <li class="active"><a href="#" title="">home</a></li>
    <li><a href="#" title="">what we do</a></li>
    <li><a href="#" title="">who we work for</a></li>
    <li><a href="#" title="">who we are</a></li>
                       <li><a href="#" title="">contact us</a></li>
    </ul>
  • Rich Green 2246 posts 4008 karma points
    Aug 12, 2009 @ 10:50
    Rich Green
    0

    Send by mistake and no edit feature here, I'll start again:

    I'm pretty sure this is very basic but I can't seem to find a working example.

    Here's our one level menu driven by XSLT

    <ul class="links">
    <li class="active"><a href="#" title="">home</a></li>
    <li><a href="#" title="">what we do</a></li>
    <li><a href="#" title="">contact us</a></li>
    </ul>

     

    We need to set the class ‘active’ to whichever page we are on.

    Any example or advice would be greatly appreciated.

    Many thanks, Rich

  • Chris Koiak 700 posts 2626 karma points
    Aug 12, 2009 @ 10:52
    Chris Koiak
    0

    You could try looking at http://our.umbraco.org/projects/cogworks---flexible-navigation

    This package will probably do just what you're looking for, and it'll help you understand xslt more.

    Cheers,

    Chris

  • Rich Green 2246 posts 4008 karma points
    Aug 12, 2009 @ 11:00
    Rich Green
    0

    Thanks for the super fast reply chris!

    I had a look your package already, it seems to deal with multi level navigation.

    However ours is just a one level menu and surely a simple task everyone has to solve?

    I was hoping for a much more straight forward solution.

    Thanks, Rich

  • Chris Koiak 700 posts 2626 karma points
    Aug 12, 2009 @ 11:11
    Chris Koiak
    0

    ah, ok. If it's only a single level then something like this should work

    <xsl:param name="currentPage"/>
    <xsl:variable name="level" select="1"/>

    <xsl:template>
    <xsl:variable name="nodeList" select="$currentPage/ancestor-or-self::node[@level=$level]/node"/>

    <ul class="links">
    <xsl:for-each select="$nodeList">
    <li>
    <xsl:if test="$currentPage/ancestor-or-self::node /@id = current()/@id">
    <xsl:text> current</xsl:text>
    </xsl:if>

    <a href="{umbraco.library:NiceUrl(current()/@id)}">
    <xsl:value-of select="current()/@nodeName"/>
    </a>
    </li>
    </xsl:for-each>
    </ul>

    </xsl:template>

    You may need to play with the 'level' param depending on your implementation.

  • Bart de Jonge 29 posts 51 karma points
    Aug 12, 2009 @ 11:20
    Bart de Jonge
    0

    hi Chris,

    I use something like this for my topnav. I think this is what you are looking for:

         <ul>
    <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:if test="$currentPage/@id = current()/@id">
    <xsl:attribute name="class">selected</xsl:attribute>
    </xsl:if>
    <xsl:if test="$currentPage/../@id = current()/@id">
    <xsl:attribute name="class">selected</xsl:attribute>
    </xsl:if>
    <span><xsl:value-of select="@nodeName"/></span>
    </a>
    </li>

    </xsl:for-each>
    </ul>
  • Rich Green 2246 posts 4008 karma points
    Aug 12, 2009 @ 11:44
    Rich Green
    0

    Neither of these examples work for me, none of the IF statements return a match.

    Here's my code with the IF statements listed above:


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

    <xsl:template match="/">

    <!-- The fun starts here -->
    <ul class="links">
    <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1']">
    <li>
    <xsl:if test="$currentPage/@id = current()/@id">
    <xsl:attribute name="class">active</xsl:attribute>
    </xsl:if>

    <xsl:if test="$currentPage/../@id = current()/@id">
    <xsl:attribute name="class">active</xsl:attribute>
    </xsl:if>

    <xsl:if test="$currentPage/ancestor-or-self::node /@id = current()/@id">
    <xsl:attribute name="class">active</xsl:attribute>
    </xsl:if>

    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:value-of select="@nodeName"/>
    </a>
    </li>
    </xsl:for-each>
    </ul>

     

    Any more ideas?

    Thanks, Rich

     

     

  • Bart de Jonge 29 posts 51 karma points
    Aug 12, 2009 @ 11:58
    Bart de Jonge
    0

    hi Rich,

    and if you set the level to 2 like this: <xsl:variable name="level" select="2"/>.

    cheers Bart

     

  • Bert 128 posts 251 karma points
    Aug 12, 2009 @ 12:18
    Bert
    0

    Could you add your Content structure? Something like:

    Content:

    - menu item to display 1
    -- some page

    - menu item to display 2

     

    And where do you want to set the class the li tag or the a tag? This code sets the class to the link element.

    <a href="{umbraco.library:NiceUrl(@id)}">
                   
    <xsl:value-of select="@nodeName"/>  
                    <xsl:if test="$currentPage/ancestor-or-self::node /@id = current()/@id">
                           
    <xsl:attribute name="class">active</xsl:attribute>
                   
    </xsl:if>
    </a>
  • Rich Green 2246 posts 4008 karma points
    Aug 12, 2009 @ 12:31
    Rich Green
    0

    Thanks, but it's still not working.

    Maybe I'm confusing people, there is no content structure, it's just one level of navigation.

    There is NO sub navigation.

    ------

    Home  Page - Home-Page.aspx

    Who we work for  - Who-We-Work-for.aspx

    Contact Us page - Contact-Us.aspx

    -------

    This code is an example if I was on the Who-We-Work-For.aspx page.

    <ul class="links">
    <li><a href="/home.aspx">Home</a></li>
    <li class="current"><a href="/who-we-work-for.aspx">Who We Work For</a></li>
    <li><a href="/contact-us.aspx">Contact Us</a></li>
    </ul>

     

    This code is an example if I was on the Home.aspx page.

    <ul class="links">
    <li class="current"><a href="/home.aspx">Home</a></li>
    <li><a href="/who-we-work-for.aspx">Who We Work For</a></li>
    <li><a href="/contact-us.aspx">Contact Us</a></li>
    </ul>

     

    I was thinking this would be pretty standard and straight forward?

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Aug 12, 2009 @ 12:43
    Douglas Robar
    101

    Ahhh! Simple indeed. I'd do something like this...

    <xsl:template>
    <ul class="links">
       
    <xsl:for-each select="umbraco.library:GetXmlAll()/node[@level='1']>
         
    <li>
         
    <xsl:if test="$currentPage/@id = current()/@id">
           
    <xsl:text> current</xsl:text>
         
    </xsl:if>

         
    <a href="{umbraco.library:NiceUrl(current()/@id)}">
             
    <xsl:value-of select="current()/@nodeName"/>
         
    </a>
         
    </li>
       
    </xsl:for-each>
    </ul>

    </xsl:template>

    cheers,
    doug.

  • Chris Koiak 700 posts 2626 karma points
    Aug 12, 2009 @ 15:48
    Chris Koiak
    0

    Just a tip for all those with similar xslt issues: Have a look at the raw xml file in data/umbraco.config.

    Once you have an understanding of this file's xml structure you will understand your xlst bugs easier.

    Cheers,

    Chris

  • Rich Green 2246 posts 4008 karma points
    Aug 12, 2009 @ 19:03
    Rich Green
    0

    Thanks Doug, that worked perfect! Cheers everyone else for their help too.

    Here's the full code for anyone interested

    <?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" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

    <xsl:output method="xml" omit-xml-declaration="yes" />
    <xsl:param name="currentPage"/>
    <xsl:template match="/">

    <ul class="links">
    <xsl:for-each select="umbraco.library:GetXmlAll()/node[@level='1']">
    <li>
    <xsl:if test="$currentPage/@id = current()/@id">
    <xsl:attribute name="class">active</xsl:attribute>
    </xsl:if>

    <a href="{umbraco.library:NiceUrl(current()/@id)}">
    <xsl:value-of select="current()/@nodeName"/>
    </a>
    </li>
    </xsl:for-each>
    </ul>

    </xsl:template>

     

     

     

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Aug 12, 2009 @ 21:22
    Douglas Robar
    0

    When you have a chance, please mark the solution.

    cheers,
    doug.

  • Rich Green 2246 posts 4008 karma points
    Aug 13, 2009 @ 09:52
    Rich Green
    0

    Have done so now thanks Doug.

    The only line I changed was

     <xsl:if test="$currentPage/@id = current()/@id">
         
    <xsl:text>current</xsl:text>
     
    </xsl:if>

    to

    <xsl:if test="$currentPage/@id = current()/@id">
         
    <xsl:attribute name="class">active</xsl:attribute>
    </xsl:if>

    Many thanks, Rich

  • Luke Johnson 61 posts 80 karma points
    Jun 27, 2011 @ 19:37
    Luke Johnson
    0

    I couldn't get this to work with  <xsl:for-eachselect="umbraco.library:GetXmlAll()/node[@level='1']">, but I did manage to make another solution work:

    http://wifi.umbraco.org/forum/core/general/15423-Link-Class-Selected-Page?p=0#comment81313

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies