tags:

views:

396

answers:

6

I proposed what I think is a better syntax for ASP.NET MVC views in this question. Since that question has been answered, I think my answer will generate little feedback, so I am posting it as its own question here.

+2  A: 

You're using markup to represent code. My opinion is: where code is needed, just use code, which is always more flexible. Where markup is needed, use markup. This article explains precisely my point. Sometimes the line between code and markup is blurry, though.

Mauricio Scheffer
I don't see how using markup to represent code is any worse than using code to represent markup, which is being promoted with the Html extension methods.
RedFilter
Thanks for the article, looks very interesting, I will definitely read it - it has piqued my interest in Lisp.
RedFilter
I also try to avoid Html extensions like these (http://mark-dot-net.blogspot.com/2008/05/using-aspnet-mvc-htmlhelperform.html) as well. I use markup when markup is needed, code when code is needed. I agree that sometimes the line is blurry.
Mauricio Scheffer
I don't like using markup to represent code because: it's unnecessarily verbose; it's not as flexible; it's an unnecessary, probably leaky, abstraction in this case.
Mauricio Scheffer
The biggest problem with the MVC Views is that if you don't use the HtmlHelpers, you can't autogenerate your links etc based on routes.
FlySwat
@jonathan: what about UrlHelper? (http://stackoverflow.com/questions/355700/aspnet-mvc-public-alternative-to-urlhelpergenerateurl)
Mauricio Scheffer
@mausch - unnecessarily verbose? it is much shorter than the code it replaces; not as flexible? that's a feature - it encourages minimal code in the view; in fact the limited code you need in a view makes it a good place to abstract - ifs, loops, and response.writes cover most of your needs.
RedFilter
@Jonathan Holland - Url.Action or similar is what I imagine you would use for this
RedFilter
+1  A: 

I really wish that people would stop treating XML as a programming language.

Matt Briggs
exactly my point
Mauricio Scheffer
I think a view is a very constrained use case and it can work very well. (I am no fan of XSL.)
RedFilter
A: 

In addition to maucsch and matt's points, wouldn't this also mean that the server would have to load into memory and parse the entire page looking for "mvc:"? And isn't that one of the reasons to not use webforms?

Daniel Schaffer
Nope, that's exactly the job of the view engine.
Mauricio Scheffer
A: 

You're on the right track, but I think you went too far. The balance is mixing the code with the html where it flows and not over complicating it and also not creating tag soup. The best view engine that I have found that does this is Spark.

Take a look at it and you will find it addresses what you are proposing in a more subtle and readable way.

Dale Ragan
Heh, I was just going to mention Spark :)
Mauricio Scheffer
Yeap, it's awesome. I have been using it on every ASP.NET MVC project so far. It does exactly what he wants to accomplish with his mvc: server control.
Dale Ragan
More readable possibly. It is definitely more text, and introduces new tags as well as new attributes. It does not seem to have the ability of rendering as is, though, so not as easy to use for HTML layout people. Also, by introducing new tags it affects the indenting of the outputted code.
RedFilter
It looks very usable though. From my current standpoint, attribute-based syntax seems more concise and non-developer friendly than tag-based.
RedFilter
It's definitely subjective, but I find the syntax to be very similar to what most CMS's use for templates. Therefore, designers that I have worked with, picked it up very easily.
Dale Ragan
Spark is great! I am a convert. It does everything the way I would have done it, except better and more :)
RedFilter
Yep, I hope the intellisense support gets better, but other than that it is great. I use it for all my MVC projects now. I don't think I will switch back. The web designers that I have worked with also like it.
Dale Ragan
A: 

Also take a look at JSP: they had to introduce an "Expression Language" in order to get some of the power of code in the jsp markup. The result is really awkward IMHO. It even needs an explicit mapping (in XML, of course) to access a simple function from this Expression Language.

See this.

Mauricio Scheffer
My point is that we should be using platforms that *discourage* complex code in the view, by providing simple constructs that tend to make you do the right thing. Loops, Ifs, and writes seem to cover the bulk of the needs, very simple.
RedFilter
My opinion on this: make simple things simple, complex things possible. Complex things are not possible with that approach, that's why JSP had to introduce EL.
Mauricio Scheffer
+1  A: 

Maybe you should use this "MVC syntax" instead, called HAML.

%h2= Model.CategoryName
%ul
  - foreach (var product in Model.Products)
    %li
      = product.ProductName 
      .editlink
        = Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID })
= Html.ActionLink("Add New Product", new { Action="New" })

replaces

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" 
    CodeBehind="List.aspx" Inherits="MvcApplication5.Views.Products.List" Title="Products" %>
<asp:Content ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
 <h2><%= ViewData.Model.CategoryName %></h2>
  <ul>
    <% foreach (var product in ViewData.Model.Products) { %>
      <li>
        <%= product.ProductName %> 
        <div class="editlink">
          (<%= Html.ActionLink("Edit", new { Action="Edit", ID=product.ProductID })%>)
        </div>
      </li>
    <% } %>
  </ul>
  <%= Html.ActionLink("Add New Product", new { Action="New" }) %>
</asp:Content>
Justice
That completely misses the point of having it be HTML that can render as standard HTML content.
RedFilter
HAML is disgusting.
FlySwat
All HTML template engines render HTML content based on non-HTML input. For some engines, the input syntax resembles HTML in all its glorious verbosity. For other engines, the input syntax is designed to be read and written by people.
Justice