Copied to clipboard

Flag this post as spam?

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


  • Jules 205 posts 448 karma points
    May 10, 2011 @ 14:59
    Jules
    0

    List Level1 and Level2 nodes in same navigation

    There must be a better way of doing what I am trying to achieve below.

    Basically I want to list Level1 and Level2 nodes in the same navigation.

    Is there a neater way to do this?

    JG

    <xsl:template match="/">
    <!-- The fun starts here -->
      <xsl:choose>
        <xsl:when test="$currentPage/@level='1'">
          <div class="nav_button_l">
            <a href="{umbraco.library:NiceUrl($currentPage/@id)}">
              <xsl:value-of select="$currentPage/@nodeName"/>
            </a>
          </div>
          <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
             <div class="nav_button_l">
               <a href="{umbraco.library:NiceUrl(@id)}">
                 <xsl:value-of select="@nodeName"/>
               </a>
             </div>
           </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
          <div class="nav_button_l">
             <a href="{umbraco.library:NiceUrl($currentPage/ancestor-or-self::* [@level=$level]/@id)}">
               <xsl:value-of select="$currentPage/ancestor-or-self::* [@level=$level]/@nodeName"/>
             </a>
           </div>
          <xsl:for-each select="$currentPage/ancestor-or-self::* [@level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
            <div class="nav_button_l">
              <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:value-of select="@nodeName"/>
              </a>
            </div>
          </xsl:for-each>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
  • Dennis Aaen 4457 posts 17970 karma points admin hq c-trib
    May 17, 2011 @ 18:57
    Dennis Aaen
    0

    Hi JG.

    Do not know if you have found a better solution to your question.

    But you asked if there was a better way to get level 1 nodes and level 2 nodes in the same navigation.

    I have tried to make a small example where I lists level 1 nodes and below each level1 nodes retrieved level 2 nodes if there are any.

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

    <xsl:template match="/">

    <!-- The fun starts here -->
    <ul>
    <xsl:for-each select="$currentPage/ancestor-or-self::* [@isDoc and @level=$level]/* [@isDoc and string(umbracoNaviHide) != '1']">
      <li>
        <a href="{umbraco.library:NiceUrl(@id)}">
          <xsl:if test="$currentPage/ancestor-or-self::*/@id = current()/@id">
            <!-- we're under the item - you can do your own styling here -->
            <xsl:attribute name="class">selected</xsl:attribute>
          </xsl:if>
          <xsl:value-of select="@nodeName"/>
        </a>
      </li>
        <xsl:apply-templates select="."></xsl:apply-templates>
    </xsl:for-each>
    </ul>

    </xsl:template>

    <xsl:template match="*">
      <xsl:for-each select="./child::*  [@isDoc and string(umbracoNaviHide) != '1']">
        <li>
          <a href="{umbraco.library:NiceUrl(@id)}">
            <!-- we're under the item - you can do your own styling here -->
            <xsl:attribute name="class">selected</xsl:attribute>
            <xsl:value-of select="@nodeName"/>
          </a>
        </li>
      </xsl:for-each>
    </xsl:template>

    Hope you can use my suggestion for a solution.

    /Dennis

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    May 17, 2011 @ 21:47
    Chriztian Steinmeier
    1

    Hi JG (+Dennis)

    XSLT is very efficient for stuff like this, but requires thinking a little bit outside of what you're used to. This is a classic example where "normal" programming would require some sort of forEach loop, which isn't really necessary for recursive stuff like this (in XSLT) - example:

    <!-- Define scope by start- and endlevels -->
    <xsl:variable name="startLevel" select="1" />
    <xsl:variable name="endLevel" select="2" />
    
    <xsl:template match="/">
        <!-- Start processing pages at $startLevel, ignoring hidden pages -->
        <xsl:apply-templates select="$currentPage/ancestor-or-self::*[@level = $startLevel][not(umbracoNaviHide = 1)]" />
    </xsl:template>
    
    <!-- Template for all links -->
    <xsl:template match="*[@isDoc]">
        <div class="nav_button_l">
            <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:if test="ancestor-or-self::*[@id = $currentPage/@id]">
                    <xsl:attribute name="class">selected</xsl:attribute>
                </xsl:if>
                <xsl:value-of select="@nodeName" />
            </a>
        </div>
        <!-- Process any page(s) below this (within selected scope) -->
        <xsl:apply-templates select="*[@isDoc][@level &lt;= $endLevel][not(umbracoNaviHide = 1)]" />
    </xsl:template>

     

    Hope this helps. 

    /Chriztian

  • Dennis Aaen 4457 posts 17970 karma points admin hq c-trib
    May 17, 2011 @ 22:47
    Dennis Aaen
    0

    Hi Chriztian,

    Thanks for your example to solve this problem. Always nice to see more ways to solve the same problem.
    Some solutions are of course more appropriate than others. And I am currently. in a learning process of writing XSLT code most advisable.

    So thank you Chriztian :-)

    /Dennis

  • Jules 205 posts 448 karma points
    May 19, 2011 @ 09:17
    Jules
    0

    Hi Chriztian

    Thanks for taking the time to do this - very informative

    regards

    Julian Grahame

  • 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