Copied to clipboard

Flag this post as spam?

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


  • Sam 184 posts 209 karma points
    Oct 01, 2010 @ 13:36
    Sam
    0

    Keeping parent and sub level navigation highlighted

    Hi everyone:) I wonder if someone could help by either tweaking my code, or just by explaining how I can do this and then I'll go away and (ahem ...try) to sort the code out, and repost the result if it's successful. I use the following for my navigation:

    <xsl:template match="/">

    <!-- start writing XSLT -->

    <ul class="mainNav">

    <!--adds home link-->

    <li>
    <xsl:if test="$currentPage/ancestor-or-self::node[@level=1]/@id = $currentPage/@id">
    <xsl:attribute name="class">
    <xsl:value-of select="'current'"/>
    </xsl:attribute>
    </xsl:if>

        <a href="{umbraco.library:NiceUrl($currentPage/ancestor-or-self::node[@level=1]/@id)}" class="mainNav-leftLink">
            <span><xsl:value-of select="$currentPage/ancestor-or-self::node[@level=1]/@nodeName"/></span>
        </a>
    </li>
    <!--adds home link end-->


    <!--generates list-->

    <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=1]/node [string(data [@alias='umbracoNaviHide']) != '1']">
        <li>

    <xsl:if test="$currentPage/@id = current()/@id">
                <xsl:attribute name="class">
                    <xsl:value-of select="'current'"/>
                </xsl:attribute>             
    </xsl:if>

    <xsl:if test="@id = $currentPage/parent::node/@id">
                <xsl:attribute name="class">
                    <xsl:value-of select="'parent'"/>
                </xsl:attribute>             
    </xsl:if>

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

    <!--generates list end-->

    </ul>

    </xsl:template>

    ... this works just fine for my purposes, top level items have <li class="current"> when selected, sub level has <li class="parent"> when sub page is selected. This is good for me as my site only goes as far as:

    www.mysite.com/page/sub-page.aspx

    However, if I go any deeper I lose the current (or parent) highlighting. Problem is I don't fully understand the code (any explanation would be fantastic ie what's the difference between $currentPage/@id and current()/@id ??). I have studied the umbraco tutorial on xpath but still a little confused.

    Thanks in advance if someone can shed some light on this. Thanks :)

  • Lachlann 344 posts 626 karma points
    Oct 04, 2010 @ 15:14
    Lachlann
    0

    Hey instead of using a for loop to iterate through all the children you could use a recursvily called template and then pass in a parameter to tell it that the node is a child of the active node.

    something like this should work, start with the first node, i.e. this might be your home page.

         <xsl:call-template name="listMenuItems">
         <xsl:with-param name="node" select="$FirstNode/child::node [@level=1]/node [string(data [@alias='umbracoNaviHide']) != '1']" />
         <xsl:with-param name="isSubChild" select="$currentPage/@id = current()/@id" />
         </xsl:call-template>

     

    Then the template would look like:

    <xsl:template name="listMenuItems">
        <xsl:param name="node" />
        <xsl:param name="isSubChild" />
    <xsl:for-each select="$
    node">
    <li>

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

         <xsl:call-template name="listMenuItems">
         <xsl:with-param name="node" select="$node/child::node [@level=1]/node [string(data [@alias='umbracoNaviHide']) != '1']" />
         <xsl:with-param name="isSubChild" select="./@id = current()/@id" />
         </xsl:call-template>

    </xsl:for-each>
    </xsl:template>

    I havn't tested this but I think this or somethign like it should technically work. The basic idea of keeping trackof whether or not the parent is the current/active page by passing a variable through a recursive tempalte should be okay.

     

    Does anyone have a better way? Is there a way of testing if a node is in a node set? I.e. testing if a node is a child of another node.

  • Sam 184 posts 209 karma points
    Oct 04, 2010 @ 21:10
    Sam
    0

    I really couldn't tell you if your version is a good solution or not, I haven't got as far as params and calling templates in macros yet, still learning :)

  • Lachlann 344 posts 626 karma points
    Oct 05, 2010 @ 18:01
    Lachlann
    0

    Thats cool,

     

    Give the the above a go and see if it works. If it does then thats all good and perhaps somone with a better way will comment.

     

  • Sam 184 posts 209 karma points
    Oct 05, 2010 @ 19:44
    Sam
    0

    Thanks :)

Please Sign in or register to post replies

Write your reply to:

Draft