Copied to clipboard

Flag this post as spam?

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


  • Robert Valcourt 70 posts 103 karma points
    Aug 10, 2015 @ 20:04
    Robert Valcourt
    0

    Disable subnav when at certain node tree

    I have a subnav XSLT script that displays all child nodes of the tree currently being viewed. This works fine. I want to 'disable' the subnav when you are viewing a certain node tree by its @id.

    For example if you are viewing:

    http://mysite.com/section1/, all of the child nodes display in the sidebar. http://mysite.com/section2/, all of the child nodes display in the sidebar. http://mysite.com/section3/, subnav display should be disabled.

    http://mysite.com/section3/ has a node @id of 1148. Whether you are viewing this node or any of its children nodes, no subnav should be displayed.

    The script basically works like this:

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

    The way I see it, an IF statement needs to be put above the xsl:for-each

    <xsl:if node being viewed or any child of 1148 is not true>
      <xsl:for-each select="$mainNodes">
        // Do something
      </xsl:for-each>
    </xsl:if>
    

    I can't seem to get the syntax right. Thoughts?

  • Per Olsson 47 posts 307 karma points
    Aug 11, 2015 @ 06:27
    Per Olsson
    0

    I would not recommend to hardcode the page ID, instead make a checkbox property on the documenttype and set it where you want to hide it.

    You should be able to modify "mainNodes" variable by simply adding the following to your select (or whatever propertyname you choose):

    and not(hideSubnav = 1) 
    
  • Robert Valcourt 70 posts 103 karma points
    Aug 11, 2015 @ 08:38
    Robert Valcourt
    0

    @Per Olsson your example will not work for me. That checks against the single node being viewed, im trying to hide an entire node tree. Basically, if you are viewing node 1148 or ANY child of, simply don't process the subnav script period.

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Aug 14, 2015 @ 22:06
    Chriztian Steinmeier
    0

    Hi Robert,

    You're actually on the right track, but the XSLT syntax you're using is for the legacy schema, which you're probably not using (Umbraco 4.1 or below, IIRC).

    Here's a chunk of code that outlines how I'd approach a similar situation - note that it handles both the "hardcoded ID" scenario you're suggesting and the hideSubnav property that Per suggested:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:output method="html" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
        <xsl:variable name="level" select="2" />
        <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = $level]" />
    
        <xsl:variable name="mainNodes" select="$siteRoot/*[@isDoc][not(umbracoNaviHide = 1)]" />
    
        <xsl:template match="/">
            <ul>
                <xsl:apply-templates select="$mainNodes" />
            </ul>
        </xsl:template>
    
        <!-- General template for a node -->
        <xsl:template match="*[@isDoc]">
            <xsl:variable name="subNodes" select="*[@isDoc][not(umbracoNaviHide = 1)]" />
            <li>
                <a href="{umb:NiceUrl(@id)}"><xsl:value-of select="@nodeName" /></a>
                <xsl:if test="$subNodes">
                    <ul>
                        <xsl:apply-templates select="$subNodes" />
                    </ul>
                </xsl:if>
            </li>
        </xsl:template>
    
        <!-- Template for a specific node that shouldn't be processed -->
        <xsl:template match="*[@id = 1148] | *[hideSubnav = 1]" priority="1">
            <!-- Do nothing -->
        </xsl:template>
    
    </xsl:stylesheet>
    

    Hope that helps,

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft