Copied to clipboard

Flag this post as spam?

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


  • Eduardo Rivas 17 posts 37 karma points
    Feb 20, 2012 @ 22:50
    Eduardo Rivas
    0

    Umbraco 5: Razor 2nd level nav

    I've been trying to port my xslt 2nd level menu to razor. The idea is to have a menu header depending on which Document Type the current page has. Also, the menu in not nested, just a clean list of the children of 2nd level pages, and when you descend to any of these children, a list of siblings. This is what I had in xslt:

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <xsl:variable name="items" select="$currentPage/ancestor-or-self::* [@isDoc and @level = 2]/* [@isDoc and string(umbracoNaviHide) != '1']"/>

    <!-- The fun starts here -->

    <xsl:if test="count($items) &gt; 0">
    <h2>
    <xsl:choose>
    <xsl:when test="$currentPage/ancestor-or-self::DocType1[@isDoc]">
    DocType1 Header
    </xsl:when>
    <xsl:when test="$currentPage/ancestor-or-self::DocType2[@isDoc]">
    DocType2 Header
    </xsl:when>
    <xsl:otherwise>
    Generic Header
    </xsl:otherwise>
    </xsl:choose>
    </h2>
    <ul>
    <xsl:for-each select="$items">
    <li>
    <a href="{umbraco.library:NiceUrl(@id)}">
    <xsl:value-of select="@nodeName"/>
    </a>
    </li>
    </xsl:for-each>
    </ul>
    </xsl:if>

    </xsl:template>
    </xsl:stylesheet>

    And this is what I have in a Razor Partial View:

    @inherits RenderViewPage
    @using Umbraco.Cms.Web;
    @{var title = "";}
    @{if(DynamicModel.ContentType.Alias == "DocType1"){
    title = "DocType1 Header";
    }
    else if(DynamicModel.ContentType.Alias == "DocType2"){
    title = "DocType2 Header";
    }
    else {
    title = "Generic Header";
    }
    }
    <h2>@title</h2>
    <ul>
    @foreach(var item in DynamicModel.Children) {
    if (@item.CurrentTemplate != null) {
    <li><a href="@item.Url">@item.Name</a></li>
    }
    }
    </ul>

    My questions are: Can the If part in Razor be optimized? (Converted to a switch or a smaller expression) And, how can I get this menu to show children in the 2nd level, but siblings in the 3rd? (Using .AncestorOrSelf, I guess, but I've only got errors with that). Thanks in advance.

Please Sign in or register to post replies

Write your reply to:

Draft