Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the benefits of using an alternate ASP.NET MVC view engine?

I have looked at the usual suspects...Spark, NHaml, etc. They all seem to be syntactic sugar for those that are uncomfortable with the <% %> syntax. Are there any other tangible benefits? Syntactic sugar doesn't seem to me to be a sufficient reason to change out the entire view engine.

Reasons posted so far:

  1. Easier to transition from a different platform
  2. More natural context switching
  3. Better separation of concerns
  4. Fewer lines of code
  5. Better resistance against cross-site scripting
  6. Better XHTML compliance
like image 430
Robert Harvey Avatar asked Jun 24 '09 21:06

Robert Harvey


2 Answers

The reason why people are uncomfortable with the <% %> syntax isn't that it contains alot of syntactic salt, but that it makes the Views code-centric, which can go against the MVC concept of making Views as dumb as possible. The goal of Spark, for example, is "to allow the html to dominate the flow and the code to fit seamlessly". So the tangible benefit is making it easier to follow the spirit of MVC.

<viewdata products="IEnumerable[[Product]]"/>
<ul if="products.Any()">
  <li each="var p in products">${p.Name}</li>
</ul>
<else>
  <p>No products available</p>
</else>

If the above is just syntactic sugar, then ASP.NET MVC itself is just syntactic sugar on top of ASP.NET Web Forms.

like image 188
bzlm Avatar answered Sep 28 '22 02:09

bzlm


From the nhaml perspective

  • make views more succinct

Nhaml view (274 chars)

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

aspx view (665 chars)

<%@ 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.CategoryName %></h2>
  <ul>
    <% foreach (var product in ViewData.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>

It does this through a series of shorthand characters. See here for a full list [http://code.google.com/p/nhaml/wiki/NHamlLanguageReference]

  • partials and layout

better to look here [http://code.google.com/p/nhaml/wiki/PartialsAndLayouts]

  • htmlencoding by default (through config) for all content to avoid XSS

  • XHTML compliant output

from the spark perspective

  • embedded code into the xml tags and custom code tags can be used to perform progromattic actions. This all allows spark to minimise the context switching that occurs for both nhaml and aspx.

for example this spark

<viewdata products="IEnumerable[[Product]]"/>
<ul if="products.Any()">
  <li each="var p in products">${p.Name}</li>
</ul>
<else>
  <p>No products available</p>
</else>

aspx and nhaml would require you do a context swtich out to code to perform the if..else statement.

  • master layout files [http://sparkviewengine.com/documentation/master-layouts]

References

[http://code.google.com/p/nhaml/wiki/NHamlLanguageReference]

[http://sparkviewengine.com/documentation/syntax]

like image 29
Simon Avatar answered Sep 28 '22 01:09

Simon