Copied to clipboard

Flag this post as spam?

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


  • James Mason 11 posts 31 karma points
    Jan 08, 2011 @ 15:02
    James Mason
    0

    Top navigation template - How to add a class to list item if the node has children?

    Hello,  I am trying to build a menu like this:

    and this is the code I am starting with Umbraco 4.5.2:

    <?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"/>

        <!-- update this variable on how deep your navigation should be -->
        <xsl:variable name="maxLevel" select="4"/>

        <xsl:template match="/">
            <ul class="dropdown dropdown-horizontal">
                <xsl:call-template name="drawNodes">
                    <xsl:with-param name="parent" select="$currentPage/ancestor-or-self::* [@level=1 and @isDoc]"/>
                </xsl:call-template>
            </ul>
        </xsl:template>

        <xsl:template name="drawNodes">
            <xsl:param name="parent"/>
            <xsl:if test="umbraco.library:IsProtected($parent/@id, $parent/@path) = 0 or (umbraco.library:IsProtected($parent/@id, $parent/@path) = 1 and umbraco.library:IsLoggedOn() = 1)">
                <xsl:for-each select="$parent/* [@isDoc and string(umbracoNaviHide) != '1' and @level &lt;= $maxLevel]">
                    <li>
                        <href="{umbraco.library:NiceUrl(@id)}">
                            <xsl:value-of select="@nodeName"/>
                        </a>
                      
                        <xsl:if test="count(./* [@isDoc and string(umbracoNaviHide) != '1' and @level &lt;= $maxLevel]) &gt; 0">
                            <ul>
                                <xsl:call-template name="drawNodes">
                                    <xsl:with-param name="parent" select="."/>
                                </xsl:call-template>
                            </ul>
                        </xsl:if>
                    </li>
                </xsl:for-each>
            </xsl:if>
        </xsl:template>
    </xsl:stylesheet>

     

    I have managed to replicate the menu, except I can not figure out how to add arrow graphics to the list items which have children.  Please could you help.

    Thanks

    James

    p.s I found a topic which poses the same question but the code is structured differently and I had trouble working it in to my code above.

    http://our.umbraco.org/forum/templating/templates-and-document-types/5769-Submenu,-has-children-then-add-class-

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jan 08, 2011 @ 15:48
    Jan Skovgaard
    0

    Hi James

    I think you just need to count the child elements of the node you're at using the same if sentence you're calling the template with if further levels exists.

    Try this out

                <xsl:for-each select="$parent/* [@isDoc and string(umbracoNaviHide) != '1' and @level &lt;= $maxLevel]">
                    <li>
                        <xsl:if test="count(./* [@isDoc and string(umbracoNaviHide) != '1' and @level &lt;= $maxLevel]) &gt; 0">
                            <xsl:attribute name="class"><xsl:text>yourclassname</xsl:text></xsl:attribute>
                        </xsl:if>

                        <a href="{umbraco.library:NiceUrl(@id)}">
                            <xsl:value-of select="@nodeName"/>
                        </a>
                     
                        <xsl:if test="count(./* [@isDoc and string(umbracoNaviHide) != '1' and @level &lt;= $maxLevel]) &gt; 0">
                            <ul>
                                <xsl:call-template name="drawNodes">
                                    <xsl:with-param name="parent" select="."/>
                                </xsl:call-template>
                            </ul>
                        </xsl:if>
                    </li>
                </xsl:for-each>

    If there is childnodes the class attribute will be set with a value of "yourclassname" :-)

    Hope this helps

    /Jan

  • James Mason 11 posts 31 karma points
    Jan 08, 2011 @ 16:17
    James Mason
    0

    Jan,

    Perfect! 

    Thankyou :)

    James

Please Sign in or register to post replies

Write your reply to:

Draft