Reputation: 15455
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
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
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
Reputation:
<div id="myResults"<%= isTrue ? ' style="display: none;"' : '' %>>
Perhaps there's an even shorter notation.
Upvotes: 0
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