1

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.

Community
  • 1
  • 1
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283

6 Answers6

2

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
  • 98,863
  • 23
  • 192
  • 275
  • 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. – D'Arcy Rittich Dec 24 '08 at 14:25
  • Thanks for the article, looks very interesting, I will definitely read it - it has piqued my interest in Lisp. – D'Arcy Rittich Dec 24 '08 at 14:27
  • 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 Dec 24 '08 at 15:20
  • 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 Dec 24 '08 at 15:25
  • 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 Dec 24 '08 at 15:25
  • @jonathan: what about UrlHelper? (http://stackoverflow.com/questions/355700/aspnet-mvc-public-alternative-to-urlhelpergenerateurl) – Mauricio Scheffer Dec 24 '08 at 15:30
  • @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. – D'Arcy Rittich Dec 24 '08 at 20:46
  • @Jonathan Holland - Url.Action or similar is what I imagine you would use for this – D'Arcy Rittich Dec 24 '08 at 20:48
1

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

Matt Briggs
  • 41,224
  • 16
  • 95
  • 126
1

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>
yfeldblum
  • 65,165
  • 12
  • 129
  • 169
  • That completely misses the point of having it be HTML that can render as standard HTML content. – D'Arcy Rittich Dec 24 '08 at 20:43
  • 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. – yfeldblum Dec 31 '08 at 05:37
0

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
  • 18,202
  • 3
  • 54
  • 70
  • Heh, I was just going to mention Spark :) – Mauricio Scheffer Dec 24 '08 at 16:29
  • 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 Dec 24 '08 at 16:36
  • 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. – D'Arcy Rittich Dec 24 '08 at 20:57
  • It looks very usable though. From my current standpoint, attribute-based syntax seems more concise and non-developer friendly than tag-based. – D'Arcy Rittich Dec 24 '08 at 20:58
  • 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 Dec 25 '08 at 17:11
  • Spark is great! I am a convert. It does everything the way I would have done it, except better and more :) – D'Arcy Rittich Feb 16 '10 at 19:56
  • 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 Feb 16 '10 at 21:07
0

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
  • 98,863
  • 23
  • 192
  • 275
  • 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. – D'Arcy Rittich Dec 24 '08 at 21:00
  • 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 Dec 25 '08 at 13:45
-2

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
  • 56,753
  • 31
  • 116
  • 165