views:

348

answers:

5

With ASP.NET's view engine/template aspx/ashx pages the way to spit to screen seems to be:

<%= Person.Name %>

Which was fine with webforms as alot of model data was bound to controls programatically. But with MVC we are now using this syntax more oftern.

The issue I have with it is quite trivial, but annoying either way. This is that it seems to break up the mark up i.e.:

<% foreach(var Person in People) { %>
    <%= Person.Name %>
<% } %>

That seems like alot of opening and closing tags to me!

Other view engines in the MVC contrib have a means of spitting to screen with out opening and closing the script tags using standard keyword such as "print, out, echo" i.e. (brail example):

<% 
for element in list:
      output "<li>${element}</li>" 
end
%>

Now, I said this may seem trivial, but it just seems more readable this way. So what are the advantages of MS having this syntax, and not providing a output method?

Cheers, Chris.

+6  A: 

Consider something like this, instead:

<% foreach(var Person in People) { 
    Response.Write(Person.Name); 
} %>

I believe that'll work. (Although I haven't tested it; I've only just begun with MVC and don't have the toolset here at the office.)

EDIT: I apparently missed the actual question ... :)

Microsoft does provide an output method, but didn't provide a syntax like the one you describe. The output method is Response.Write(). I can't answer this directly (I believe you'll need to check with Scott Hanselmann over at MS :)), but I think they didn't want to complicate scripting by adding yet-another-language for us to learn; I think they wanted to leverage the languages (C#, VB, etc.) which developers already knew.

EDIT #2: I placed the following in a comment, and in retrospect (for completeness), it should be part of the answer.

If you head over to the Learn MVC site on ASP.NET, in the view tutorial (that's the link), you'll see the following paragraph:

Since you call Response.Write() so often, Microsoft provides you with a shortcut for calling the Response.Write() method. The view in Listing 3 uses the delimiters <%= and %> as a shortcut for calling Response.Write().

Essentially, <%= %> is the accepted shortcut for Response.Write, and you therefore can use the full Response.Write method anywhere you'd use <%= %>.

John Rudy
That works perfectly.
Adhip Gupta
Although, you will need a semi colon at the end of the Response.Write Statement.
Adhip Gupta
My bad! I'll update again. :)
John Rudy
A: 

What you're looking for is probably a different View engine -- they each handle embedded code like that in their own way. Check out NHaml, an ASP.Net port of Rails' Haml engine.

Some more view engines to look at: Brail, NVelocity

Adam Lassek
A: 

Thanks guys,I dont specifically want to switch view engines, though I will take time to look at the other avaliable. I am sure you can understand that most MS development houses dont touch anything but MS branded technology so I just wanted to know if there was a way to do this with the default view engine. Or if not, why not? Is there a reason, if not where could I suggest the default view compiler add such a keywork.

John- I believe that Response.Write(Person.Name) will just render the string to the top of the page, before any other output. Not 100% on that though, ill give it a bash.

Edit:

Apparently, Response.Write(Person.Name) works as suggested. Well that was easy. Thanks :-) John.

Owen
Check http://www.asp.net/learn/mvc/tutorial-04-cs.aspx. <%= %> is a shortcut for Response.Write.
John Rudy
A: 

I've heard they're working on improving this for ASP.Net 4 via client templates.

Joel Coehoorn
A: 

Another option other than Response.Write would be to write XHtml with XSL transformations

Adam Lassek
True, but why make it more complex than it needs to be?
John Rudy
I didn't say it was a good option :) Using a different View engine would be much preferable to XSLT. But who knows; maybe somewhere out there there's a situation to which XSLT is ideally suited. It could happen.
Adam Lassek