Rod
Rod

Reputation: 15455

other ways to write this markup

Is there another way to write the preceding? The only difference is displaying it or not based on a condition.

Thanks, rodchar

Upvotes: 0

Views: 77

Answers (4)

David
David

Reputation: 1927

since your using styles, you could get it out of the body of the html altogether...

<style type="text/css">
div#myResults { display: <$= isTrue ? "none" : "block" %>; }
</style>
...
<div id=myResults>

Upvotes: 1

Tomas Aschan
Tomas Aschan

Reputation: 60584

Even though Jörns example will work, you might want to consider putting this in an extension method:

<%= Html.Results(isTrue) %>

And in a class in your library:

public static class MyHtmlExtensions

    public static string Results(this HtmlHelper helper, bool hidden)
    {
        return String.Format("<div id=""myResults"" {0}>",
                             hidden = ? "style=""display: none;""" : "");
    }
}

Upvotes: 1

user217782
user217782

Reputation:

<div id="myResults"<%= isTrue ? ' style="display: none;"' : '' %>>

Perhaps there's an even shorter notation.

Upvotes: 0

J&#248;rn Schou-Rode
J&#248;rn Schou-Rode

Reputation: 38346

Here's another way, but I am not sure I like it better than the original ;)

<div id="myResults"<%= isTrue ? " style=\"display: none;\"" : "" %>>

If you want something that is both concise and readable, consider switching to the NHaml view engine:

#myResults{ style = isTrue ? "display: none" : null }

Upvotes: 6

Related Questions