Copied to clipboard

Flag this post as spam?

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


  • Garrett Fisher 341 posts 496 karma points
    Aug 15, 2012 @ 16:31
    Garrett Fisher
    0

    Immediate Preceding & Following Siblings Hidden in Menu

    Hi,

    I have an interesting challenge wherein I want to show Prev and Next Links along a level but ONLY where umbracoNaviHide is false.  The reason I'm having trouble is that in order to get the immediate one I have to specify the first ([1]), but the fact is that the preceding or following siblings may or may not have umbracoNaviHide set.  So, the result is that my links only show IF the that particular sibling IS shown in the menu.  Here is my faulty code:

    <xsl:if test="$currentPage/@level &gt;= 4">
      
      <div style="clear:both;padding-top:75px;">
        <xsl:if test="normalize-space($currentPage/preceding-sibling::node()[1][not(umbracoNaviHide = 1)]/@id)">
        <div style="float:left;width:50%;">
          <a href="{umbraco.library:NiceUrl($currentPage/preceding-sibling::node()[1][not(umbracoNaviHide = 1)]/@id)}">
            &#171;
            Prev:
            <xsl:value-of select="$currentPage/preceding-sibling::node()[1][not(umbracoNaviHide = 1)]/@nodeName"/>
          </a>
        </div>
        </xsl:if>
        <xsl:if test="normalize-space($currentPage/following-sibling::node()[1][not(umbracoNaviHide = 1)]/@id)">
        <div style="float:right;width:50%;">
          <a href="{umbraco.library:NiceUrl($currentPage/following-sibling::node()[1][not(umbracoNaviHide = 1)]/@id)}">
            Next:
            <xsl:value-of select="$currentPage/following-sibling::node()[1][not(umbracoNaviHide = 1)]/@nodeName"/>
            &#187;
           </a>
        </div>
        </xsl:if>
      </div>
    </xsl:if>

    How can I code it so that it only "looks for" the IMMEDIATE NEXT or PREVIOUS sibling which *IS* shown in the menu?  In other words, if we're talking about Next, then if the immediate next one is not shown in the menu, then go to the next one and so on until we find one that's umbracoNaviHide != 1.  Otherwise print nothing.

    Is that clear?

    Thanks in advance,

    Garrett

  • Garrett Fisher 341 posts 496 karma points
    Aug 15, 2012 @ 17:01
    Garrett Fisher
    0

    Hey, I got this working so I'm posting the solution:

    <xsl:if test="$currentPage/@level &gt;= 4">
      
      <xsl:variable name="previousNodes" select="$currentPage/preceding-sibling::node()[@level = $currentPage/@level and not(umbracoNaviHide = 1)]"/>
      <xsl:variable name="followingNodes" select="$currentPage/following-sibling::node()[@level = $currentPage/@level and not(umbracoNaviHide = 1)]"/>
      
      <xsl:if test="count($previousNodes) &gt; 0 or count($followingNodes) &gt; 0">
        <div style="clear:both;padding-top:75px;">
        
          <xsl:if test="count($previousNodes) &gt; 0">
            <div style="float:left;width:50%;">
            <xsl:for-each select="$previousNodes">
              <xsl:if test="normalize-space(@id) and position() = last()">
                <a href="{umbraco.library:NiceUrl(@id)}">
                  &#171;
                  Prev:
                  <xsl:value-of select="@nodeName"/>
                </a>
              </xsl:if>
            </xsl:for-each>
            </div>
          </xsl:if>
          <xsl:if test="count($followingNodes) &gt; 0">
            <div style="float:right;width:50%;">
            <xsl:for-each select="$followingNodes">
              <xsl:if test="normalize-space(@id) and position() = 1">
                <a href="{umbraco.library:NiceUrl(@id)}">
                  Next:
                  <xsl:value-of select="@nodeName"/>
                  &#187;
                </a>
              </xsl:if>
            </xsl:for-each>
            </div>
          </xsl:if>
        
        </div>
      </xsl:if>

    </xsl:if>

    Thanks for all who took a look at it.

    //Garrett

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 15, 2012 @ 19:13
    Chriztian Steinmeier
    2

    Hi Garrett,

    Nice to see you found a way to do it - just because I'm a little pedantic with XSLT, here's how you can make the selection directly in the XPath, which is what you really wanted to know, I'm sure :-)

    <xsl:if test="$currentPage/@level &gt;= 4]">
    
        <xsl:variable name="prevNode" select="$currentPage/preceding-sibling::*[@isDoc][not(umbracoNaviHide = 1)][1]" />
        <xsl:variable name="nextNode" select="$currentPage/following-sibling::*[@isDoc][not(umbracoNaviHide = 1)][1]" />
    
        <xsl:if test="$prevNode or $nextNode">
            <div>
                <xsl:if test="$prevNode">
                    <div>
                        <a href="{umbraco.library:NiceUrl($prevNode/@id)}">
                            <xsl:text>&#171; Prev: </xsl:text>
                            <xsl:value-of select="$prevNode/@nodeName" />
                        </a>                  
                    </div>
                </xsl:if>
    
                <xsl:if test="$nextNode">
                    <div>
                        <a href="{umbraco.library:NiceUrl($nextNode/@id)}">
                            <xsl:text>Next: </xsl:text>
                            <xsl:value-of select="$nextNode/@nodeName" />
                            <xsl:text> &#187;</xsl:text>
                        </a>                  
                    </div>
                </xsl:if>
            </div>
        </xsl:if>
    
    </xsl:if>

     

    /Chriztian

  • Garrett Fisher 341 posts 496 karma points
    Aug 16, 2012 @ 03:13
    Garrett Fisher
    0

    Pedantic my a**, Chriztian! 

    You are always the bomb -- you've opened my eyes to WAY more code-efficient code solutions on numerous occasions.  You're the best XSLT expert of whom I know, and I am sure I'm not alone.  Seriously.  And best part of it, you always respond.  You're an OSH.  An Open Source Hero.  Everyone knows it.

    As usual, your code is shorter again.  I don't think you've ever typed <xsl:for-each in your natural life.  Genius.  Don't need a loop because we only need one node.  Done.  Using your code.

    Thanks as always C,

    Garrett

  • Nigel Wilson 945 posts 2077 karma points
    Aug 16, 2012 @ 04:35
    Nigel Wilson
    0

    Hi Garrett

    A wicked response by Chriztian - maybe mark his response as the solution - I have high fived him due to the excellent answer....

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Aug 16, 2012 @ 08:25
    Chriztian Steinmeier
    0

    Hehe - awesome thing to wake up to - that, and Jim Dalrymple's "Rabid Beaver" laugh on the "Amplified" podcast. My day is made, that's for sure :-)

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft