Question

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
Was it helpful?

Solution

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.

OTHER TIPS

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.

References

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

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

Syntactic sugar in what way? So that you can learn yet another syntax? No. These engines are of great use to developers migrating from other platforms. Makes their life much easier.

Yes, see HTML generator for .NET?

You want the same refactorability you have in your normal c# code. It is code, so you want to be able to structure it the same way. Use inheritance, composition, parameters, loops,recursion, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top