Copied to clipboard

Flag this post as spam?

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


  • Eddie Foreman 215 posts 288 karma points
    Jul 15, 2010 @ 12:46
    Eddie Foreman
    0

    Traverse up tree to add class

    Hi All

    Been working on the following xslt, which is generating the menu correctly.  The next step is to add a current class, to the <a> tag of the parent <li> of the nested <ul> which contains the the current page. 

    Home
     - Level 2 - If nested <ul> contains current page, set <a> tag  class of this <li> to current
        - Level 3 page
        - Level 3 page - current page
        - Level 3 page

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

    <xsl:template match="/">

      <ul>
        <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=$level]/node [string(data [@alias='umbracoNaviHide']) != '1' and string(data[@alias='showInFooter']) != '1']">
          <li>
            <xsl:attribute name="class">
              <xsl:value-of select="Exslt.ExsltStrings:lowercase(umbraco.library:Replace(@nodeName, ' ', ''))"/>
            </xsl:attribute>
            <a href="{umbraco.library:NiceUrl(@id)}">
              <xsl:attribute name="title">
                <xsl:value-of select="@nodeName"/>
              </xsl:attribute>
              <xsl:value-of select="@nodeName"/> 
            </a>
            <xsl:if test="count(node) &gt; 0">                  
                  <ul>
                    <xsl:attribute name="class">
                      <xsl:value-of select="Exslt.ExsltStrings:lowercase(umbraco.library:Replace(@nodeName, ' ', ''))"/>
                      <xsl:text>SubMenu</xsl:text>
                    </xsl:attribute>
                    <xsl:for-each select="node">
                      <li>
                        <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
                          <xsl:attribute name="class">
                            <xsl:text>current</xsl:text>
                          </xsl:attribute>
                        </xsl:if>

                        <xsl:attribute name="class">
                          <xsl:text>sub</xsl:text>
                          <xsl:value-of select="Exslt.ExsltStrings:lowercase(umbraco.library:Replace(@nodeName, ' ', ''))"/>
                        </xsl:attribute>
                        <a href="{umbraco.library:NiceUrl(@id)}">
                          <xsl:attribute name="title">
                            <xsl:value-of select="@nodeName"/>
                          </xsl:attribute>
                          <xsl:value-of select="@nodeName"/>
                        </a>
                      </li>
                    </xsl:for-each>
                  </ul>
            </xsl:if>
          </li>
        </xsl:for-each>
      </ul>
    </xsl:template>

    Not sure how to traverse back up the tree, any help would be appericated. 

    Working with Umbraco 4.0.4.2

    Thanks

    Eddie

     

     

     

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Jul 15, 2010 @ 13:04
    Chriztian Steinmeier
    0

     

    Hi Eddie,

     

    Actually, you don't need to traverse up the tree, but instead you check if the branch you're on contains $currentPage:

    <li class="{Exslt.ExsltStrings:lowercase(umbraco.library:Replace(@nodeName, ' ', ''))}">
        <a href="{umbraco.library:NiceUrl(@id)}" title="{@nodeName}">
            <!-- Check if currentPage is somewhere below this -->
            <xsl:if test="descendant::node[@id = $currentPage/@id]">
                <xsl:attribute name="class">current</xsl:attribute>
            </xsl:if>
            <xsl:value-of select="@nodeName"/>  
        </a>
    
        ...
    
    </li>

    Note also, that I've collapsed the generation of the title attribute - you don't need to use <xsl:attribute> for the simple title attributes you're creating.

    /Chriztian

  • Eddie Foreman 215 posts 288 karma points
    Jul 15, 2010 @ 13:18
    Eddie Foreman
    0

    Hi Chriztian

    Your a star, and a thanks for the tips.

    Eddie

  • 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