Renerdering whole Content tree for top nav Options
awaegel
Posted: Saturday, August 16, 2008 8:25:09 AM
Rank: Newbie

Joined: 6/28/2008
Posts: 8
Hello,

Beginner Umbraco user here, but am a Sitecore-certified developer, and notice an amazing number of similarities between the platforms. Must be a coincidence.

Anyway, I'm building a top nav menu where I just want to render the whole tree of Content items under 'Home' as nested unordered lists. Umbraco xsl seems to work a little differently that what I'm used to, so I want to ask if there's a better way to do it than this:

Code:

<xsl:param name="parent">Home</xsl:param>

<xsl:template match="/">
    <xsl:call-template name="menu">
    <xsl:with-param name="parent" select="$parent"/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="menu">

    <xsl:param name="parent"/>
    <ul>

    <!-- add css class for topmost ul -->
    <xsl:if test="$parent='Home'">
        <xsl:attribute name="class">top_menu</xsl:attribute>
    </xsl:if>

    <!-- iterate over all elements of the current level -->
    <xsl:for-each select="$currentPage/ancestor::root//node[@nodeName=$parent]/node [string(data [@alias='umbracoNaviHide']) != '1']">
        <li>
          <a href="{umbraco.library:NiceUrl(@id)}">
            <xsl:if test="$currentPage/ancestor-or-self::node/@id = current()/@id">
              <xsl:attribute name="class">nav_selected</xsl:attribute>
            </xsl:if>
            <xsl:value-of select="@nodeName"/>
          </a>

         
          <!-- test for children, recurse if found -->   
          <xsl:if test="count(descendant::node [string(data [@alias='umbracoNaviHide']) != '1']) > 0">
            <xsl:call-template name="menu"><xsl:with-param name="parent" select="@nodeName"/></xsl:call-template>
          </xsl:if>

        </li>
      </xsl:for-each>
    </ul>

</xsl:template>


Thanks,
- Andrew
ddrayne
Posted: Monday, August 18, 2008 5:06:51 PM
Rank: Newbie

Joined: 7/28/2008
Posts: 17
Location: Edinburgh
Hi there

try looking at the "sitemap" xslt.


Code:

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

<!-- update this variable on how deep your site map should be -->
<xsl:variable name="maxLevelForSitemap" select="5"/>

<xsl:template match="/">
    <div id="main_nav">
        <xsl:call-template name="drawNodes"> 
            <xsl:with-param name="parent" select="$currentPage/ancestor-or-self::node [@level=1]"/> 
        </xsl:call-template>
    </div>
</xsl:template>

<xsl:template name="drawNodes">
    <xsl:param name="parent"/>
    <xsl:if test="umbraco.library:IsProtected($parent/@id, $parent/@path) = 0 or (umbraco.library:IsProtected($parent/@id, $parent/@path) = 1 and umbraco.library:IsLoggedOn() = 1)">
        <ul>
            <xsl:for-each select="$parent/node [string(./data [@alias='umbracoNaviHide']) != '1' and @level &lt;= $maxLevelForSitemap]">
                <xsl:if test="(string(@level) = '2') and (position() = 1)">
                    <li style="margin-left:8px;"><a class="homelink" href="{umbraco.library:NiceUrl(1070)}" >Home</a></li>
                </xsl:if>       
                <li> 
                    <a href="{umbraco.library:NiceUrl(@id)}">
                    <xsl:value-of select="@nodeName"/></a> 
                    <xsl:if test="count(./node [string(./data [@alias='umbracoNaviHide']) != '1' and @level &lt;= $maxLevelForSitemap]) &gt; 0">   
                        <xsl:call-template name="drawNodes">   
                        <xsl:with-param name="parent" select="."/>   
                        </xsl:call-template> 
                    </xsl:if>
                </li>
            </xsl:for-each>
        </ul>
    </xsl:if>
</xsl:template>
</xsl:stylesheet>


That should do it, or at least be a good starting point.
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.