Copied to clipboard

Flag this post as spam?

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


  • Nathan Woulfe 422 posts 1580 karma points MVP 3x c-trib
    Sep 21, 2012 @ 07:27
    Nathan Woulfe
    0

    Help with conditional menu

    Hi 

    I'm trying to build out a menu and need a little advice - my Xslt experience is a bit limited... What I've got so far is a multi-level nested menu that renders everything from level 2 to level 4 of the site, relative to the current page (ie, if the current page is level 2, the menu shows all level 2 pages, if it's level 3, it shows all pages at level 2 and all level 3 siblings of the current page).

    I'd like to extend this to allow a node to have a property (say, topLevelMenuItem) which overrides the default menu layout - if topLevelMenuItem is true for the current page, the menu will only show the child nodes of the current page. The node with topLevelMenuItem set true essentially becomes a level 1 node, and the menu begins with its children.

    I'm setting two variables:

     

    <xsl:variable name="subMenuTopLevelNodes" select="$currentPage/ancestor-or-self::* [topLevelMenuItem = 1]/* [string(umbracoNaviHide) != '1' and @isDoc]"/>
    <xsl:variable name="subMenuLevelTwoNodes" select="$currentPage/ancestor-or-self::* [@level = 2]/* [string(umbracoNaviHide) != '1' and @isDoc]"/>

     

    If subMenuTopLevelMenuNodes.Count() is zero, I render the full menu, which works fine. How would I handle the second scenario?

    The menu template looks like this:

      <xsl:template name="submenuTree">
        <xsl:param name="pageNodes"/>
        <xsl:for-each select="$pageNodes">
          <li>
            <xsl:choose>
              <xsl:when test="count(current()/*[string(umbracoNaviHide) != '1' and @isDoc]) &gt; 0">
                <xsl:choose>
                  <xsl:when test="$currentPage/@level &gt;= current()/@level and @id = $currentPage/ancestor-or-self::*[@level=current()/@level]/@id">
                    <xsl:attribute name="class">show-children selected</xsl:attribute>
                    <xsl:if test="$currentPage/@id = @id">
                      <xsl:attribute name="class">show-children selected</xsl:attribute>
                    </xsl:if>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:attribute name="class">has-children</xsl:attribute>
                    <xsl:if test="$currentPage/@id = @id">
                      <xsl:attribute name="class">has-children selected</xsl:attribute>
                    </xsl:if>
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
              <xsl:otherwise>
                <xsl:if test="$currentPage/@id = @id">
                  <xsl:attribute name="class">selected</xsl:attribute>
                </xsl:if>
              </xsl:otherwise>
            </xsl:choose>
            <a href="{umbraco.library:NiceUrl(@id)}">
              <xsl:value-of select="@nodeName"/>
            </a>
           <xsl:if test="$currentPage/@level &gt;= current()/@level and @id = $currentPage/ancestor-or-self::*[@level = current()/@level]/@id and count(current()/*[string(umbracoNaviHide) != '1' and @isDoc]) &gt; 0 and current()/@level &lt; 5">
              <ul>
                <xsl:call-template name="submenuTree">
                  <xsl:with-param name="pageNodes" select="$currentPage/ancestor-or-self::*[@level = current()/@level]/*[string(umbracoNaviHide) != '1' and @isDoc]"/>
                </xsl:call-template>
              </ul>
            </xsl:if>
          </li>
        </xsl:for-each>
      </xsl:template>

    Any suggestions/direction/advice would be greatly appreciated...

     

    ta

    Nathan

  • Nathan Woulfe 422 posts 1580 karma points MVP 3x c-trib
    Sep 24, 2012 @ 03:02
    Nathan Woulfe
    1

    Solved this one, if anyone is interested - absolutely a resutl of my lack of Xslt experience. I'm now using apply-templates rather than call-template, with the code block below controlling which menu tree is displayed:

          <xsl:choose>
            <xsl:when test="count($currentPage/ancestor-or-self::* [topLevelMenuItem = 1]) &gt; 0">
              <xsl:apply-templates select="$currentPage/ancestor-or-self::* [topLevelMenuItem = 1]/* [string(umbracoNaviHide) != '1' and @isDoc]" />       
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="$currentPage/ancestor-or-self::* [@level = 2]/* [string(umbracoNaviHide) != '1' and @isDoc]" />
            </xsl:otherwise>
          </xsl:choose>

  • 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