Copied to clipboard

Flag this post as spam?

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


  • ben 18 posts 38 karma points
    Aug 07, 2009 @ 00:17
    ben
    0

    XSLT Menu Problem - Newbie!

    Hello there, I am running the latest Umbraco 4.0.2.1 on windows xp, .NET 3.5.

    I have a menu, where by there are top level menu items and some of these pages can have children, and i only want to show these children if the parent page is selected, but i cant get the if condition to work properly here is my xslt:

    At the moment it does get out the children for the page that has children but when clicking on any other page it renders the rest of the top level pages as sub level ones, i am very new to umbraco and xslt!

    Please help!

     

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

    <xsl:template match="/">

    <ul id="navi">

    <xsl:variable name="rootNode" select="$currentPage/ancestor-or-self::node [@level=1]" />

    <xsl:variable name="rootTextpageNode" select="$currentPage/ancestor-or-self::node [@level = 2 and @nodeTypeAlias = 'MainPage']" />

    <li>

    <a href="{umbraco.library:NiceUrl($rootNode/@id)}">
    <!--
    Add the class selected if the root node ID matches our
    current node ID in the for each loop
    -->
    <xsl:if test="$rootNode/@id = $currentPage/@id">
    <xsl:attribute name="class">
    <xsl:text>
    on
    </xsl:text>
    </xsl:attribute>
    </xsl:if>
    <xsl:value-of select="$rootNode/@nodeName" />
    </a>

    </li>



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

    <xsl:if test="@nodeName = 'Contact'">
    <xsl:attribute name="class">
    <xsl:text>
    contact
    </xsl:text>
    </xsl:attribute>
    </xsl:if>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <!--
    Add the class selected if the currentpage or parent nodes (up the tree to the root)
    ID matches our current node ID in the for each loop
    -->
    <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
    <xsl:attribute name="class">
    <xsl:text>
    on
    </xsl:text>
    </xsl:attribute>
    </xsl:if>
    <xsl:value-of select="@nodeName" />

    </a>
    <xsl:if test="$rootTextpageNode/@id = current()/@id">
    <ul>
    <xsl:for-each select="$rootTextpageNode/node">
    <li>
    <a href="{umbraco.library:NiceUrl(current()/@id)}">
    <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
    <xsl:attribute name="class">
    <xsl:text>on</xsl:text>
    </xsl:attribute>
    </xsl:if>
    <span>
    <xsl:value-of select="current()/@nodeName"/>
    </span>
    </a>
    </li>
    </xsl:for-each>
    </ul>
    </xsl:if>
    </li>

    </xsl:for-each>

    </ul>

    </xsl:template>

    </xsl:stylesheet>
  • Masood Afzal 176 posts 522 karma points
    Aug 07, 2009 @ 00:30
    Masood Afzal
    0

    Have a look at this package which can be used for same purpose.

    Cogworks - Flexible Navigation

    Link: http://our.umbraco.org/projects/cogworks---flexible-navigation

     

     

     

  • Masood Afzal 176 posts 522 karma points
    Aug 07, 2009 @ 00:35
    Masood Afzal
    0

    Another sample is here:

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

    <!-- Input the documenttype you want here -->
    <!-- Typically '1' for topnavigtaion and '2' for 2nd level -->
    <!-- Use div elements around this macro combined with css -->
    <!-- for styling the navigation -->
    <xsl:variable name="level" select="1"/>

    <xsl:template match="/">

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

    </xsl:template>

    </xsl:stylesheet>
  • ben 18 posts 38 karma points
    Aug 07, 2009 @ 01:00
    ben
    0

    Thank you for your help, I have installed the package from your first post and it works exactly how i want it to apart from it doesnt show my home page node at the top, any idea why?

     

     

  • ben 18 posts 38 karma points
    Aug 07, 2009 @ 01:15
    ben
    0

    My content site map in umbraco is like this:

    Home
         About us
               child page
         Contact us
         Web

    Etc

    And the menu just pulls in from about us down when the startingLevel = 1

    Any ideas?

     

  • Masood Afzal 176 posts 522 karma points
    Aug 07, 2009 @ 11:01
    Masood Afzal
    0

    Following xslt should display homepage link.

    <

    ul>

    <

    xsl:attribute name="class">

    <

    xsl:value-of select="concat($ulBaseClass, ' lv', '1')" disable-output-escaping="yes"/>

    </

    xsl:attribute>

    <

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

    <

    li>

    <

    xsl:if test="@nodeName=$currentPage/@nodeName">

    <

    xsl:attribute name="class">

    <

    xsl:value-of select="concat($selectedClass,' ')" disable-output-escaping="yes"/>

    </

    xsl:attribute>

    </

    xsl:if>

    <

    a href="{umbraco.library:NiceUrl(@id)}">

    <

    xsl:value-of select="@nodeName"/>

    </

    a>

    </

    li>

    </

    xsl:for-each>

    </

    ul>

  • ben 18 posts 38 karma points
    Aug 07, 2009 @ 12:13
    ben
    0

    Thank you once again for helping me out, i put the code in and it did get out the home link, all be it in a seperate ul li list, i tried just pasting in the section that does the li but it wouldnt parse the file, also when i put all of it in, and then clicked on another page it put home as a child of that page.

     

    Any ideas?

     

    Thanks again in advance

  • dandrayne 1138 posts 2262 karma points
    Aug 07, 2009 @ 12:24
    dandrayne
    0

    You're always going to want a home link, so why not just hard-code it?

    <a class="homelink" href="{umbraco.library:NiceUrl($currentPage/ancestor-or-self::node [@level=1]/@id)}">
    <xsl:value-of select="umbraco.library:NiceUrl($currentPage/ancestor-or-self::node [@level=1]/@nodeName" />
    </a>

     

  • Masood Afzal 176 posts 522 karma points
    Aug 07, 2009 @ 12:33
    Masood Afzal
    0

    What about changing your xslt template to something like below (as mentioned by dandrayne):

    <

    xsl:template match="/">

    <!--

    Navigation including homepage link -->

    <

    ul>

    <

    xsl:attribute name="class">

    <

    xsl:value-of select="concat($ulBaseClass, ' lv', '0')" disable-output-escaping="yes"/>

    </

    xsl:attribute>

    <

    li>

    <

    xsl:if test="@nodeName=$currentPage/@nodeName">

    <

    xsl:attribute name="class">

    <

    xsl:value-of select="concat($selectedClass,' ')" disable-output-escaping="yes"/>

    </

    xsl:attribute>

    </

    xsl:if>

    <

    a class="homelink" href="{umbraco.library:NiceUrl($currentPage/ancestor-or-self::node [@level=1]/@id)}">

    <

    xsl:value-of select="$currentPage/ancestor-or-self::node [@level=1]/@nodeName" />

    </

    a>

    <

    xsl:call-template name="nodeIterator">

    <

    xsl:with-param name="parentNode" select="$currentPage/ancestor-or-self::node[@level=$startDepth] [string(data [@alias='umbracoNaviHide']) != '1']" />

    <

    xsl:with-param name="pseudoCurrentPage" select="$currentPage" />

    </

    xsl:call-template>

    </

    li>

    </

    ul>

    </

    xsl:template>

     

  • ben 18 posts 38 karma points
    Aug 07, 2009 @ 13:22
    ben
    0

    Hi guys, i have tried putting in both your code snippets but i cant get them to work, just get a error reading xslt file here is my code that is from the package above which works that doesnt have the home link in, if anyone could change the code and show me where it should go?

    I want it as the top <li> in the list.

    thanks once again!

     

    <?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="html" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage" />

    <!--This sets the level that the nav starts at and tells us if we should recurse through child elements-->
    <xsl:variable name="startDepth" select="/macro/startingLevel" />
    <xsl:variable name="recurse" select="/macro/recurse" />
    <xsl:variable name="selectBranches" select="/macro/selectBranches"></xsl:variable>
    <xsl:variable name="maxMenuDepth" select="/macro/maxMenuDepth"></xsl:variable>
    <!--Alternate page title variable in here-->

    <!--Styles for the navigation-->
    <xsl:variable name="ulBaseClass" select="/macro/ulBaseClass"></xsl:variable>
    <xsl:variable name="branchClass" select="/macro/branchClass"></xsl:variable>
    <xsl:variable name="selectedClass" select="/macro/selectedClass"></xsl:variable>

    <!--This calls first iteration of the navigation, sending the first node at the correct depth found in the ancestors of the current page-->
    <xsl:template match="/">
    <xsl:call-template name="nodeIterator">
    <xsl:with-param name="parentNode" select="$currentPage/ancestor-or-self::node[@level=$startDepth] [string(data [@alias='umbracoNaviHide']) != '1']" />
    <xsl:with-param name="pseudoCurrentPage" select="$currentPage" />
    </xsl:call-template>
    </xsl:template>

    <xsl:template name="nodeIterator">
    <xsl:param name="parentNode" />
    <xsl:param name="pseudoCurrentPage" />
    <!-- do not show info doc node types-->

    <xsl:variable name="calculatedMenuDepth" select="($parentNode/@level - $startDepth)+1"></xsl:variable>

    <ul>
    <xsl:attribute name="class">
    <xsl:choose>
    <xsl:when test="$calculatedMenuDepth = 1">
    <xsl:value-of select="concat($ulBaseClass, ' lv', $calculatedMenuDepth)" />
    </xsl:when>
    <xsl:when test="$calculatedMenuDepth > 1">
    <xsl:value-of select="concat('lv', $calculatedMenuDepth)" />
    </xsl:when>
    </xsl:choose>
    </xsl:attribute>

    <!--for each node in the parent node that is not hidden by Umbraco-->
    <xsl:for-each select="$parentNode/node [string(data [@alias='umbracoNaviHide']) != '1']">

    <!--Set the current node id i.e. the node we have looped to not the current page-->
    <xsl:variable name="currentNodeID" select="./@id" />

    <!--Is the node a branch? i.e. are there children to and it is in the colletion of ancestor nodes -->
    <xsl:variable name="isBranch">
    <xsl:choose>
    <xsl:when test="$pseudoCurrentPage/ancestor-or-self::node[@id = $currentNodeID]/child::node">1</xsl:when>
    </xsl:choose>
    </xsl:variable>

    <!--Is the node selected? i.e. is it the same as the currentPage node-->
    <xsl:variable name="isSelected">
    <xsl:choose>
    <xsl:when test="$pseudoCurrentPage/@id = $currentNodeID">1</xsl:when>
    </xsl:choose>
    </xsl:variable>

    <xsl:variable name="isSelectedBranch">
    <xsl:choose>
    <xsl:when test="$isBranch = 1 and $selectBranches = 1">1</xsl:when>
    </xsl:choose>
    </xsl:variable>

    <li>

    <!-- Create the class for the li element-->
    <xsl:attribute name="class">
    <xsl:if test="$isSelected = 1"> <xsl:value-of select="concat($selectedClass,' ')"/> </xsl:if>
    <xsl:if test="$isSelectedBranch = 1"> <xsl:value-of select="concat($branchClass,' ')"/> </xsl:if>
    </xsl:attribute>

    <a href="{umbraco.library:NiceUrl(@id)}">

    <xsl:attribute name="class">
    <xsl:if test="$isSelected = 1"> <xsl:value-of select="concat($selectedClass,' ')"/> </xsl:if>
    <xsl:if test="$isSelectedBranch = 1"> <xsl:value-of select="concat($branchClass,' ')"/> </xsl:if>
    </xsl:attribute>

    <!--set the innerText for the a element-->
    <xsl:value-of select="./data[@alias='contentPageTitle']/text()"/>

    <xsl:if test="string(./data[@alias='contentPageTitle']/text()) = ''">
    <xsl:value-of select="@nodeName"/>
    </xsl:if>
    </a>

    <!-- if it's a branch recurse through it's children-->
    <xsl:if test="($isBranch = 1 and $recurse = 1 and $maxMenuDepth &gt;= $calculatedMenuDepth)">
    <xsl:call-template name="nodeIterator">
    <xsl:with-param name="parentNode" select="." />
    <xsl:with-param name="pseudoCurrentPage" select="$pseudoCurrentPage" />
    </xsl:call-template>
    </xsl:if>

    </li>

    </xsl:for-each>

    </ul>
    </xsl:template>

    </xsl:stylesheet>
  • Masood Afzal 176 posts 522 karma points
    Aug 07, 2009 @ 13:26
    Masood Afzal
    0

    Here you go. Its works fine at my end.

    <!

    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="html" omit-xml-declaration="yes"/>

    <

    xsl:param name="currentPage" />

    <!--

    This sets the level that the nav starts at and tells us if we should recurse through child elements-->

    <

    xsl:variable name="startDepth" select="/macro/startingLevel" />

    <

    xsl:variable name="recurse" select="/macro/recurse" />

    <

    xsl:variable name="selectBranches" select="/macro/selectBranches"></xsl:variable>

    <

    xsl:variable name="maxMenuDepth" select="/macro/maxMenuDepth"></xsl:variable>

    <!--

    Alternate page title variable in here-->

    <!--

    Styles for the navigation-->

    <

    xsl:variable name="ulBaseClass" select="/macro/ulBaseClass"></xsl:variable>

    <

    xsl:variable name="branchClass" select="/macro/branchClass"></xsl:variable>

    <

    xsl:variable name="selectedClass" select="/macro/selectedClass"></xsl:variable>

    <!--

    This calls first iteration of the navigation, sending the first node at the correct depth found in the ancestors of the current page-->

     

    <

    xsl:template match="/">

    <!--

    Navigation including homepage link -->

    <

    ul>

    <

    xsl:attribute name="class">

    <

    xsl:value-of select="concat($ulBaseClass, ' lv', '0')" disable-output-escaping="yes"/>

    </

    xsl:attribute>

    <

    li>

    <

    xsl:if test="@nodeName=$currentPage/@nodeName">

    <

    xsl:attribute name="class">

    <

    xsl:value-of select="concat($selectedClass,' ')" disable-output-escaping="yes"/>

    </

    xsl:attribute>

    </

    xsl:if>

    <

    a class="homelink" href="{umbraco.library:NiceUrl($currentPage/ancestor-or-self::node [@level=1]/@id)}">

    <

    xsl:value-of select="$currentPage/ancestor-or-self::node [@level=1]/@nodeName" />

    </

    a>

    <

    xsl:call-template name="nodeIterator">

    <

    xsl:with-param name="parentNode" select="$currentPage/ancestor-or-self::node[@level=$startDepth] [string(data [@alias='umbracoNaviHide']) != '1']" />

    <

    xsl:with-param name="pseudoCurrentPage" select="$currentPage" />

    </

    xsl:call-template>

    </

    li>

    </

    ul>

    </

    xsl:template>

     

    <

    xsl:template name="nodeIterator">

    <

    xsl:param name="parentNode" />

    <

    xsl:param name="pseudoCurrentPage" />

    <!--

    do not show info doc node types-->

    <

    xsl:variable name="calculatedMenuDepth" select="($parentNode/@level - $startDepth)+1"></xsl:variable>

    <

    ul>

    <

    xsl:attribute name="class">

    <

    xsl:choose>

    <

    xsl:when test="$calculatedMenuDepth = 1">

    <

    xsl:value-of select="concat($ulBaseClass, ' lv', $calculatedMenuDepth)" />

    </

    xsl:when>

    <

    xsl:when test="$calculatedMenuDepth > 1">

    <

    xsl:value-of select="concat('lv', $calculatedMenuDepth)" />

    </

    xsl:when>

    </

    xsl:choose>

    </

    xsl:attribute>

    <!--

    for each node in the parent node that is not hidden by Umbraco-->

    <

    xsl:for-each select="$parentNode/node [string(data [@alias='umbracoNaviHide']) != '1']">

    <!--

    Set the current node id i.e. the node we have looped to not the current page-->

    <

    xsl:variable name="currentNodeID" select="./@id" />

    <!--

    Is the node a branch? i.e. are there children to and it is in the colletion of ancestor nodes -->

    <

    xsl:variable name="isBranch">

    <

    xsl:choose>

    <

    xsl:when test="$pseudoCurrentPage/ancestor-or-self::node[@id = $currentNodeID]/child::node">1</xsl:when>

    </

    xsl:choose>

    </

    xsl:variable>

    <!--

    Is the node selected? i.e. is it the same as the currentPage node-->

    <

    xsl:variable name="isSelected">

    <

    xsl:choose>

    <

    xsl:when test="$pseudoCurrentPage/@id = $currentNodeID">1</xsl:when>

    </

    xsl:choose>

    </

    xsl:variable>

    <

    xsl:variable name="isSelectedBranch">

    <

    xsl:choose>

    <

    xsl:when test="$isBranch = 1 and $selectBranches = 1">1</xsl:when>

    </

    xsl:choose>

    </

    xsl:variable>

    <

    li>

    <!--

    Create the class for the li element-->

    <

    xsl:attribute name="class">

    <

    xsl:if test="$isSelected = 1">

    <

    xsl:value-of select="concat($selectedClass,' ')"/>

    </

    xsl:if>

    <

    xsl:if test="$isSelectedBranch = 1">

    <

    xsl:value-of select="concat($branchClass,' ')"/>

    </

    xsl:if>

    </

    xsl:attribute>

    <

    a href="{umbraco.library:NiceUrl(@id)}">

    <

    xsl:attribute name="class">

    <

    xsl:if test="$isSelected = 1">

    <

    xsl:value-of select="concat($selectedClass,' ')"/>

    </

    xsl:if>

    <

    xsl:if test="$isSelectedBranch = 1">

    <

    xsl:value-of select="concat($branchClass,' ')"/>

    </

    xsl:if>

    </

    xsl:attribute>

    <!--

    set the innerText for the a element-->

    <

    xsl:value-of select="./data[@alias='contentPageTitle']/text()"/>

    <

    xsl:if test="string(./data[@alias='contentPageTitle']/text()) = ''">

    <

    xsl:value-of select="@nodeName"/>

    </

    xsl:if>

    </

    a>

    <!--

    if it's a branch recurse through it's children-->

    <

    xsl:if test="($isBranch = 1 and $recurse = 1 and $maxMenuDepth &gt;= $calculatedMenuDepth)">

    <

    xsl:call-template name="nodeIterator">

    <

    xsl:with-param name="parentNode" select="." />

    <

    xsl:with-param name="pseudoCurrentPage" select="$pseudoCurrentPage" />

    </

    xsl:call-template>

    </

    xsl:if>

    </

    li>

    </

    xsl:for-each>

    </

    ul>

    </

    xsl:template>

    </

    xsl:stylesheet>

  • ben 18 posts 38 karma points
    Aug 07, 2009 @ 13:43
    ben
    0

    That xslt creates the following output for me:

     

    <ul class=" lv0">
      <li><a class="homelink" href="/home.aspx">Home</a>
          <ul class=" lv1">
              <li class="on  "><a href="/about-us.aspx" class="on  ">About Us</a>
                <ul class="lv2">
                      <li class=""><a href="/about-us/ben.aspx" class="">Ben</a></li>
                </ul>
              </li>
      <li class=""><a href="/web.aspx" class="">Web</a></li>
      <li class=""><a href="/print.aspx" class="">Print</a></li>
      <li class=""><a href="/portfolio.aspx" class="">Portfolio</a></li>
      <li class=""><a href="/contact.aspx" class="">Contact</a></li>
      <li class=""><a href="/news.aspx" class="">News</a></li>
        </ul>
      </li>
    </ul>

    Which isnt quite right?!

  • Masood Afzal 176 posts 522 karma points
    Aug 07, 2009 @ 13:56
    Masood Afzal
    0

    You can rearrange/change it in xslt template.

  • ben 18 posts 38 karma points
    Aug 07, 2009 @ 14:08
    ben
    0

    But every time i change it or move things around i get an error, apologies for this but until the other day ive never used xslt

  • Masood Afzal 176 posts 522 karma points
    Aug 07, 2009 @ 14:09
    Masood Afzal
    0

    This is another simple version. Its not based on above navigation package. It requires no parameters etc.

     

    <?

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

    <!--

    Typically '1' for topnavigtaion and '2' for 2nd level -->

    <!--

    Use div elements around this macro combined with css -->

    <!--

    for styling the navigation -->

    <

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

    <

    xsl:template match="/">

    <!--

    The fun starts here -->

    <

    ul>

    <

    li>

    <

    a href="{umbraco.library:NiceUrl($currentPage/ancestor-or-self::node [@level=1]/@id)}">

    <

    xsl:value-of select="$currentPage/ancestor-or-self::node [@level=1]/@nodeName" />

    </

    a>

    </

    li>

    <

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

    <

    li>

    <

    a href="{umbraco.library:NiceUrl(@id)}">

    <

    xsl:if test="$currentPage/ancestor-or-self::node/@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>

    <

    xsl:if test="($currentPage/@id=current()/@id and count(current()/node) > 0) or ($currentPage/@parentID = ./@id)">

    <

    ul>

    <

    xsl:for-each select="current()/node">

    <

    li>

    <

    a href="{umbraco.library:NiceUrl(@id)}">

    <

    xsl:if test="$currentPage/ancestor-or-self::node/@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:for-each>

    </

    ul>

    </

    xsl:if>

    </

    li>

    </

    xsl:for-each>

    </

    ul>

    </

    xsl:template>

    </

    xsl:stylesheet>

  • ben 18 posts 38 karma points
    Aug 07, 2009 @ 14:15
    ben
    0

    I cant seem to get anything to work now =(

    i would like to use the cog menu but just add in the homepage link in an <li> with no extra <ul>

    I dont know what im doing wrong?

  • ben 18 posts 38 karma points
    Aug 07, 2009 @ 14:36
    ben
    0

    Am i right in thinking that in the code it calls other parts of it to perform the loops etc? is that why when i change or paste code in places it errors?

  • Masood Afzal 176 posts 522 karma points
    Aug 07, 2009 @ 14:38
    Masood Afzal
    0

    This is cogs navigation altered for you:

    <?

    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="html" omit-xml-declaration="yes"/>

    <

    xsl:param name="currentPage" />

    <!--

    This sets the level that the nav starts at and tells us if we should recurse through child elements-->

    <

    xsl:variable name="startDepth" select="/macro/startingLevel" />

    <

    xsl:variable name="recurse" select="/macro/recurse" />

    <

    xsl:variable name="selectBranches" select="/macro/selectBranches"></xsl:variable>

    <

    xsl:variable name="maxMenuDepth" select="/macro/maxMenuDepth"></xsl:variable>

    <!--

    Alternate page title variable in here-->

     

    <!--

    Styles for the navigation-->

    <

    xsl:variable name="ulBaseClass" select="/macro/ulBaseClass"></xsl:variable>

    <

    xsl:variable name="branchClass" select="/macro/branchClass"></xsl:variable>

    <

    xsl:variable name="selectedClass" select="/macro/selectedClass"></xsl:variable>

    <!--

    This calls first iteration of the navigation, sending the first node at the correct depth found in the ancestors of the current page-->

    <

    xsl:template match="/">

    <!--

    Navigation including homepage link -->

    <

    xsl:call-template name="nodeIterator">

    <

    xsl:with-param name="parentNode" select="$currentPage/ancestor-or-self::node[@level=$startDepth] [string(data [@alias='umbracoNaviHide']) != '1']" />

    <

    xsl:with-param name="pseudoCurrentPage" select="$currentPage" />

    <

    xsl:with-param name="includeHomePage" select="string('1')" />

    </

    xsl:call-template>

    </

    xsl:template>

    <

    xsl:template name="nodeIterator">

    <

    xsl:param name="parentNode" />

    <

    xsl:param name="pseudoCurrentPage" />

    <

    xsl:param name="includeHomePage" />

    <!--

    do not show info doc node types-->

    <

    xsl:variable name="calculatedMenuDepth" select="($parentNode/@level - $startDepth)+1"></xsl:variable>

     

    <

    ul>

    <

    xsl:attribute name="class">

    <

    xsl:choose>

    <

    xsl:when test="$calculatedMenuDepth = 1">

    <

    xsl:value-of select="concat($ulBaseClass, ' lv', $calculatedMenuDepth)" />

    </

    xsl:when>

    <

    xsl:when test="$calculatedMenuDepth > 1">

    <

    xsl:value-of select="concat('lv', $calculatedMenuDepth)" />

    </

    xsl:when>

    </

    xsl:choose>

    </

    xsl:attribute>

    <!--

    show homepage link? -->

    <

    xsl:if test="$includeHomePage = 1">

    <

    li>

    <

    xsl:if test="@nodeName=$currentPage/@nodeName">

    <

    xsl:attribute name="class">

    <

    xsl:value-of select="concat($selectedClass,' ')" disable-output-escaping="yes"/>

    </

    xsl:attribute>

    </

    xsl:if>

    <

    a href="{umbraco.library:NiceUrl($currentPage/ancestor-or-self::node [@level=1]/@id)}">

    <

    xsl:value-of select="$currentPage/ancestor-or-self::node [@level=1]/@nodeName" />

    </

    a>

    </

    li>

    </

    xsl:if>

     

    <!--

    for each node in the parent node that is not hidden by Umbraco-->

    <

    xsl:for-each select="$parentNode/node [string(data [@alias='umbracoNaviHide']) != '1']">

    <!--

    Set the current node id i.e. the node we have looped to not the current page-->

    <

    xsl:variable name="currentNodeID" select="./@id" />

    <!--

    Is the node a branch? i.e. are there children to and it is in the colletion of ancestor nodes -->

    <

    xsl:variable name="isBranch">

    <

    xsl:choose>

    <

    xsl:when test="$pseudoCurrentPage/ancestor-or-self::node[@id = $currentNodeID]/child::node">1</xsl:when>

    </

    xsl:choose>

    </

    xsl:variable>

    <!--

    Is the node selected? i.e. is it the same as the currentPage node-->

    <

    xsl:variable name="isSelected">

    <

    xsl:choose>

    <

    xsl:when test="$pseudoCurrentPage/@id = $currentNodeID">1</xsl:when>

    </

    xsl:choose>

    </

    xsl:variable>

    <

    xsl:variable name="isSelectedBranch">

    <

    xsl:choose>

    <

    xsl:when test="$isBranch = 1 and $selectBranches = 1">1</xsl:when>

    </

    xsl:choose>

    </

    xsl:variable>

    <

    li>

     

    <!--

    Create the class for the li element-->

    <

    xsl:attribute name="class">

    <

    xsl:if test="$isSelected = 1"> <xsl:value-of select="concat($selectedClass,' ')"/> </xsl:if>

    <

    xsl:if test="$isSelectedBranch = 1"> <xsl:value-of select="concat($branchClass,' ')"/> </xsl:if>

    </

    xsl:attribute>

     

    <

    a href="{umbraco.library:NiceUrl(@id)}">

     

    <

    xsl:attribute name="class">

    <

    xsl:if test="$isSelected = 1"> <xsl:value-of select="concat($selectedClass,' ')"/> </xsl:if>

    <

    xsl:if test="$isSelectedBranch = 1"> <xsl:value-of select="concat($branchClass,' ')"/> </xsl:if>

    </

    xsl:attribute>

    <!--

    set the innerText for the a element-->

    <

    xsl:value-of select="./data[@alias='bodyTitle']/text()"/>

    <

    xsl:if test="string(./data[@alias='bodyTitle']/text()) = ''">

    <

    xsl:value-of select="@nodeName"/>

    </

    xsl:if>

    </

    a>

    <!--

    if it's a branch recurse through it's children-->

    <

    xsl:if test="($isBranch = 1 and $recurse = 1 and $maxMenuDepth &gt;= $calculatedMenuDepth)">

    <

    xsl:call-template name="nodeIterator">

    <

    xsl:with-param name="parentNode" select="." />

    <

    xsl:with-param name="pseudoCurrentPage" select="$pseudoCurrentPage" />

    <

    xsl:with-param name="includeHomePage" select="string('0')" />

    </

    xsl:call-template>

    </

    xsl:if>

    </

    li>

    </

    xsl:for-each>

    </

    ul>

    </

    xsl:template>

    </

    xsl:stylesheet>

  • Masood Afzal 176 posts 522 karma points
    Aug 07, 2009 @ 14:39
    Masood Afzal
    0

    It will display homepage link as first <li> item

  • ben 18 posts 38 karma points
    Aug 07, 2009 @ 14:53
    ben
    0

    This is FANTASTIC thank you very much!!!!!

    I am now trying to code in the condition if the page is selected so far i have this and doesnt work....

    <xsl:if test="$isSelected = $currentPage/@id">
    <xsl:attribute name="class">
    <xsl:text>
    on
    </xsl:text>
    </xsl:attribute>
    </xsl:if>
  • Masood Afzal 176 posts 522 karma points
    Aug 07, 2009 @ 15:06
    Masood Afzal
    0

    You do not need to change this code for selcted nodes. its the work. Just pass selected css class name as macro parameter e.g. "on"

     

     

    <umbraco:Macro ID="m_navigation" runat="server"

     

    startingLevel="1"

     

    recurse="1"

     

    selectBranches="1"

     

    maxMenuDepth="4"

     

    ulBaseClass="vertical"

     

    branchClass="branch"

     

    selectedClass="on"

     

    Alias="ID.Navigation"

     

     

    />

  • ben 18 posts 38 karma points
    Aug 07, 2009 @ 15:09
    ben
    0

    Right ok, I have done that already, every other page in the menu when selected turns on but putting the css class of on onto the <a> but when i am on the home link it doesnt put any class on.....?

  • Masood Afzal 176 posts 522 karma points
    Aug 07, 2009 @ 15:21
    Masood Afzal
    1

    Following code well do the job.

    <?

    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="html" omit-xml-declaration="yes"/>

    <

    xsl:param name="currentPage" />

    <

    xsl:variable name="homePage" select="$currentPage/ancestor-or-self::node[@level = 1]" />

     

    <!--

    This sets the level that the nav starts at and tells us if we should recurse through child elements-->

    <

    xsl:variable name="startDepth" select="/macro/startingLevel" />

    <

    xsl:variable name="recurse" select="/macro/recurse" />

    <

    xsl:variable name="selectBranches" select="/macro/selectBranches"></xsl:variable>

    <

    xsl:variable name="maxMenuDepth" select="/macro/maxMenuDepth"></xsl:variable>

    <!--

    Alternate page title variable in here-->

     

    <!--

    Styles for the navigation-->

    <

    xsl:variable name="ulBaseClass" select="/macro/ulBaseClass"></xsl:variable>

    <

    xsl:variable name="branchClass" select="/macro/branchClass"></xsl:variable>

    <

    xsl:variable name="selectedClass" select="/macro/selectedClass"></xsl:variable>

    <!--

    This calls first iteration of the navigation, sending the first node at the correct depth found in the ancestors of the current page-->

    <

    xsl:template match="/">

    <!--

    Navigation including homepage link -->

    <

    xsl:call-template name="nodeIterator">

    <

    xsl:with-param name="parentNode" select="$currentPage/ancestor-or-self::node[@level=$startDepth] [string(data [@alias='umbracoNaviHide']) != '1']" />

    <

    xsl:with-param name="pseudoCurrentPage" select="$currentPage" />

    <

    xsl:with-param name="includeHomePage" select="string('1')" />

    </

    xsl:call-template>

    </

    xsl:template>

    <

    xsl:template name="nodeIterator">

    <

    xsl:param name="parentNode" />

    <

    xsl:param name="pseudoCurrentPage" />

    <

    xsl:param name="includeHomePage" />

    <!--

    do not show info doc node types-->

    <

    xsl:variable name="calculatedMenuDepth" select="($parentNode/@level - $startDepth)+1"></xsl:variable>

     

    <

    ul>

    <

    xsl:attribute name="class">

    <

    xsl:choose>

    <

    xsl:when test="$calculatedMenuDepth = 1">

    <

    xsl:value-of select="concat($ulBaseClass, ' lv', $calculatedMenuDepth)" />

    </

    xsl:when>

    <

    xsl:when test="$calculatedMenuDepth > 1">

    <

    xsl:value-of select="concat('lv', $calculatedMenuDepth)" />

    </

    xsl:when>

    </

    xsl:choose>

    </

    xsl:attribute>

    <!--

    show homepage link? -->

    <

    xsl:if test="$includeHomePage = 1">

    <

    li>

    <

    xsl:if test="$homePage/@nodeName=$currentPage/@nodeName">

    <

    xsl:attribute name="class">

    <

    xsl:value-of select="concat($selectedClass,' ')"/>

    </

    xsl:attribute>

    </

    xsl:if>

    <

    a href="{umbraco.library:NiceUrl($homePage/@id)}">

    <

    xsl:attribute name="class">

    <

    xsl:if test="$homePage/@nodeName=$currentPage/@nodeName">

    <

    xsl:value-of select="concat($selectedClass,' ')"/>

    </

    xsl:if>

    </

    xsl:attribute>

    <

    xsl:value-of select="$homePage/@nodeName"/>

    </

    a>

    </

    li>

    </

    xsl:if>

     

    <!--

    for each node in the parent node that is not hidden by Umbraco-->

    <

    xsl:for-each select="$parentNode/node [string(data [@alias='umbracoNaviHide']) != '1']">

    <!--

    Set the current node id i.e. the node we have looped to not the current page-->

    <

    xsl:variable name="currentNodeID" select="./@id" />

    <!--

    Is the node a branch? i.e. are there children to and it is in the colletion of ancestor nodes -->

    <

    xsl:variable name="isBranch">

    <

    xsl:choose>

    <

    xsl:when test="$pseudoCurrentPage/ancestor-or-self::node[@id = $currentNodeID]/child::node">1</xsl:when>

    </

    xsl:choose>

    </

    xsl:variable>

    <!--

    Is the node selected? i.e. is it the same as the currentPage node-->

    <

    xsl:variable name="isSelected">

    <

    xsl:choose>

    <

    xsl:when test="$pseudoCurrentPage/@id = $currentNodeID">1</xsl:when>

    </

    xsl:choose>

    </

    xsl:variable>

    <

    xsl:variable name="isSelectedBranch">

    <

    xsl:choose>

    <

    xsl:when test="$isBranch = 1 and $selectBranches = 1">1</xsl:when>

    </

    xsl:choose>

    </

    xsl:variable>

    <

    li>

     

    <!--

    Create the class for the li element-->

    <

    xsl:attribute name="class">

    <

    xsl:if test="$isSelected = 1"> <xsl:value-of select="concat($selectedClass,' ')"/> </xsl:if>

    <

    xsl:if test="$isSelectedBranch = 1"> <xsl:value-of select="concat($branchClass,' ')"/> </xsl:if>

    </

    xsl:attribute>

     

    <

    a href="{umbraco.library:NiceUrl(@id)}">

     

    <

    xsl:attribute name="class">

    <

    xsl:if test="$isSelected = 1"> <xsl:value-of select="concat($selectedClass,' ')"/> </xsl:if>

    <

    xsl:if test="$isSelectedBranch = 1"> <xsl:value-of select="concat($branchClass,' ')"/> </xsl:if>

    </

    xsl:attribute>

    <!--

    set the innerText for the a element-->

    <

    xsl:value-of select="./data[@alias='bodyTitle']/text()"/>

    <

    xsl:if test="string(./data[@alias='bodyTitle']/text()) = ''">

    <

    xsl:value-of select="@nodeName"/>

    </

    xsl:if>

    </

    a>

    <!--

    if it's a branch recurse through it's children-->

    <

    xsl:if test="($isBranch = 1 and $recurse = 1 and $maxMenuDepth &gt;= $calculatedMenuDepth)">

    <

    xsl:call-template name="nodeIterator">

    <

    xsl:with-param name="parentNode" select="." />

    <

    xsl:with-param name="pseudoCurrentPage" select="$pseudoCurrentPage" />

    <

    xsl:with-param name="includeHomePage" select="string('0')" />

    </

    xsl:call-template>

    </

    xsl:if>

    </

    li>

    </

    xsl:for-each>

    </

    ul>

    </

    xsl:template>

    </

    xsl:stylesheet>

     

  • ben 18 posts 38 karma points
    Aug 07, 2009 @ 15:36
    ben
    0

    You sir are a genius! Thank you!

    I also thank you for your patience with me!

    I have one final question...

    Is there a way when you are on a child page to turn the parent on ?

    Thank you again :)

  • ben 18 posts 38 karma points
    Aug 07, 2009 @ 22:23
    ben
    0

    If anyone could help with this last thing i would be very grateful

    Please!!!?

  • Tommy Poulsen 514 posts 708 karma points
    Aug 07, 2009 @ 22:48
    Tommy Poulsen
    0

    Hi ben, two ways of doing this is to:

    1. Add the umbracoRedirect property alias to your document type with a content picker and you can then allow choose a node ID that you want the page to redirect to (from the wiki)

    2. Using inline ASP.NET scripting in the header part of the (master) page:
          <% Response.Redirect("/mypage.aspx"); %> 

    >Tommy

  • Tommy Poulsen 514 posts 708 karma points
    Aug 07, 2009 @ 22:49
    Tommy Poulsen
    0

    ... assuming you mean redirecting by "turning on", that is

  • ben 18 posts 38 karma points
    Aug 07, 2009 @ 23:11
    ben
    0

    Hello, what i am trying to do is use the xlst script that Masood Afzal has kindly altered for me, to turn on (in css) the parent page from the child that you are on so like:

    Home
    About Us - css class = on
         Sub page of about us - css class also on

    So as that when you are on a sub page you know what section you are in, does that make sense?

  • Masood Afzal 176 posts 522 karma points
    Aug 07, 2009 @ 23:59
    Masood Afzal
    0

    Hi Ben,

    Set the css class for 'branchClass' parameter of macro and this will do the trick.

    <umbraco:Macro ID="m_navigation" runat="server" startingLevel="1" recurse="1" selectBranches="1" maxMenuDepth="4" ulBaseClass="vertical"

     

    branchClass="branch" selectedClass="on"

    Alias="ID.Navigation" />

     

     

     

    selectedClass is current node.

    branchClass is parent node of selected node.

    You can set both to your css class e.g. "on"

     

     

  • ben 18 posts 38 karma points
    Aug 08, 2009 @ 00:14
    ben
    0

    Thank you

    Thank you

    Thank you

    Thank you

    Thank you

    =)

     

     

     

     

  • Masood Afzal 176 posts 522 karma points
    Aug 08, 2009 @ 00:17
    Masood Afzal
    0

    Please remember to mark correct answers as "Mark as helpful reply" (Thumb up icon on right side) so other users can jump to correct answer.

     

  • ben 18 posts 38 karma points
    Aug 08, 2009 @ 18:21
    ben
    0

    Says i cant vote for them as dont have enough karma points!

Please Sign in or register to post replies

Write your reply to:

Draft