Copied to clipboard

Flag this post as spam?

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


  • Sebastian Dammark 583 posts 1407 karma points
    Dec 04, 2011 @ 23:44
    Sebastian Dammark
    0

    Need a little razor help ...

    I have the following code:

    <li class="@(isActive ? "active" : "")">
    <href="@node.Url">@node.Name</a>
    </li>

    But this gives kinda dirty code since all other LI than the active on will get an empty class attribute.

    I would solve it like this with XSLT:

    <xsl:variable name="classes">
        <xsl:if test="@id = $currentPage/@id">
            <xsl:text>active </xsl:text>
        </xsl:if>
        <xsl:if test="position() = 1">
            <xsl:text>first </xsl:text>
        </xsl:if>
        <xsl:if test="position() = last()">
            <xsl:text>last </xsl:text>
        </xsl:if>
    </xsl:variable>
    
    <li>
        <xsl:if test="normalize-space($classes)">
            <xsl:attribute name="class">
                <xsl:value-of select="normalize-space($classes)" />
            </xsl:attribute>
        </xsl:if>
        <a href="{umb:NiceUrl(@id)}">
            <xsl:call-template name="pagename" />
        </a>
    </li>

    And how to I handle the multiple classes from the XSLT in the Razor example ?

    I hope that anyone can answer my question ...

  • skiltz 501 posts 701 karma points
    Dec 05, 2011 @ 04:25
    skiltz
    0

    Just move the class="" into the if statement?

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Dec 05, 2011 @ 09:50
    Sebastiaan Janssen
    1

    It doesn't really matter that it has an empty class, but you could just make your code a little uglier: 

    <li @(isActive ? "class=\"active\"" : "")>
            <a href="@node.Url">@node.Name</a>
    </li>
  • Sebastian Dammark 583 posts 1407 karma points
    Dec 05, 2011 @ 11:04
    Sebastian Dammark
    0

    Thanx alot guys.

    You're right that it doesn't really matter, but it doesn't look nice with the empty class attributes.

    How do I add a second class like 'first' or 'last' without overwriting the one 'active' currently assigned ?

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Dec 05, 2011 @ 11:46
    Sebastiaan Janssen
    0

    I would just change it up a little bit then (untested):

    @{
    var cssClass = ""; if (isActive) { cssClass = "active"; } if (node.IsFirst()) { cssClass += " first"; } if (node.IsLast()) { cssClass += " last"; } var liClass = ""; if(cssClass != string.Empty) { liClass = string.Format("class=\"{0}\"", cssClass.Trim()); } } <li @Html.Raw(liClass)> <a href="@node.Url">@node.Name</a> </li> 

    A few notes: I do the "Trim()" there because a node can be first or last, but not active, so you would get class=" first" instead of class="first".

    Because there is quotes in the string, Razor does automatic escaping, so we need to do Html.Raw to get the unescaped quotes back.

Please Sign in or register to post replies

Write your reply to:

Draft