views:

1462

answers:

2

What pros(positive sides) of using Spark view engine for ASP.NET MVC project. Why it better then default view engine?

+22  A: 

One important thing about Spark View engine is that its syntax is very similar to HTML syntax, that way your views will be clean and you will avoid "tag soup" that is in WebForms View engine. here is an example:

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>

WebForms:

<%var products = (IEnumerable<Product>)ViewData["products"] %>
<% if (products.Any()) %>
<ul>
<% foreach (var p in products) { %>
<li><%=p.Name %></li>
</ul>
<%} }  %>
<% else { %>
      <p>No products available</p>
<% }%>
Marwan Aouida
WOW, that is pretty
Dinah
Can u add some more examples?
Quintin Par
@Quintin-Par you can find more examples on the official website:http://sparkviewengine.com/documentation/expressions
Marwan Aouida
While that WebForms code could be made cleaner (see Razzie's answer), what's especially enlightening is that it's hard to spot the compilation and logic errors. The output, after adding a missing semicolon, will give you a `</ul>` for every product. Spark makes it easier to fall into the pit of success.
Thomas G. Mayfield
+8  A: 

It avoids the HTML tag soup you see a lot. Consider Spark:

<ul>
  <li each='var p in ViewData.Model.Products'>
    ${p.Name}
  </li>  
</ul>

as opposed to the classic html tag soup variant:

<ul>
  <% foreach(var p in ViewData.Model.Products) { %>
  <li>
    <%= p.Name %>
  </li>
  <% } %>
</ul>

The Spark syntax is much cleaner.

Razzie