views:

123

answers:

1

I've found a pattern in my Views like this:

<% if (someCondition)
   {
       Response.Write(string.Format("Foo {0}, Bar {1} Baz {2}.", userName, someCounter, someDate)); }
   else
   {
       Response.Write(string.Format("Foo is {0}.", bar));
   } 
%>

The basic pattern of if...else with a bunch of repeated Response.Write(string.Format()) for each condition. The idea here is not re-usability in where a partial view or helper method would be appropriate, but rather a shortcut that would ideally look like Response.WriteFormattedString().

The question here is around DRY and Response.Write(string.Format()). Are there better, or more concise ways to . Consider that HTML encoding would be a nice feature to include, perhaps as a boolean to a method call of some kind (extension method on Html?.

The goal is to try to avoid multiple render blocks <%: %> and <%= %>.

Is there an obvious extension method that I'm missing? Do you have an extension method that you rely on to achieve this functionality?

+2  A: 
<%: Html.Greeting(Model) %>

...Where Html.Greeting is a method you write which accepts your page model.

Craig Stuntz