What are the benefits of using an alternate view engine?
Asked Answered
K

5

5

I am playing with ASP.NET MVC and I see that there are a few alternate view engines available for it such as NHaml and Spark. My question is why would you use an alternate view engine? I don't see a benefit to having something like this:

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

using the Spark view engine (doubly so since, and I haven't used Spark to verify this and might be totally wrong, you wouldn't get Intellisense since you're passing the code as a string) and:

<% if products.Any() { %>
    <ul>
      <% foreach (var p in products) { %>
        <li><%= p.Name %></li>
      <% } %>  
    </ul>
<% } else { %>
    <p>No products available</p>
<% } %>

using the built-in ASP.NET MVC template format (although I admit the dangling curly brace is pretty ugly). Is there any legitimate reason apart from not like the "gator" tags (or dangling curly braces) to consider using an alternate view engine? Or is it just cool because it's something new?

Kauffmann answered 11/11, 2009 at 23:18 Comment(1)
I think the answer is embedded in the question (no, I've never used spark, but I think I will try it after seeing this comparison).Bodice
S
5

Try to make it a bit more complex:

<div if="orders.Any()" each="var order in orders">
  Here's your order #${orderIndex+1}:
  <ul>
    <li each="var p in order.Products">
       ${pIndex}: ${p.Name}
       <span if="pIsLast"> (the end)</span>
    </li>
  </ul>
</div>

I can see the flow here. I can actually see HTML here. Now take a look:

<% if (orders.Any()) { %>
  <% var orderIndex = 0; foreach (var order in orders") { %>
   <div>
    Here's your order #<%= (orderIndex+1) %>
    <ul>
      <% int pIndex = 0; foreach (var p in order.Products) 
         { bool pIsLast = pIndex == products.Count; %>
        <li>
           <%= pIndex %>: <%= p.Name %>
           <% if (pIsLast) { %>
              <span> (the end)</span>
           <% } %>
        </li>
      <% ++ pIndex; } %>  
    </ul>
   </div>
  <% orderIndex++; } %>
<% } %>

I'm lost here. Is there any HTML there?

For me, this is the main reason. Of course, Spark gives TONS of features - macros (where you code your Html.Helpers in spark markup), PDF export, etc - listed by others - but as a programmer I prefer clean code.

As another example, do you often use for (int i = 0; i < products.Count; i++) { Product product = products[i]; .... } these days? Would you prefer foreach (var product in products) {}? I would. Not only because it's less to type. Because:

  • it expresses the intent better
  • it reads better
  • it hides additional details (like .Count, .Length, or .Count(); or if it's IEnumerable that you have to traverse in a special way) from my tired mind
  • it reduces number of variables cluttering the context (and my tired mind)
  • thus I can concentrate on the problem, nothing goes in the way
  • it helps to avoid {} since there's no need to have a variable inside - reducing line count
  • and when you change the collection from IList to array, you don't change 50 loops all over the place

and that's about such simple thing as foreach. Each reason is simple, but they sum up to something bigger. And these reasons apply perfectly to Spark. Hey, looks like I'm in love ;-)

Update: now, you know what? Look at my post's edit history. I had to edit the damn ASP code several times just because I missed some bits here and there. I just do not see if it's right or wrong. It's completely hidden if I have the span there, or condition at place.

Update: hm, yet another edit... moving ASP's if/span inside li...

Snatch answered 19/11, 2009 at 21:55 Comment(0)
C
2

I think the question you need to ask is "why you want to change view engines" rather than "Should I change view engines"

I chose spark because I wanted cleaner looking views and I also wanted to use it to create templates for things other than HTML. I currently use it for generating XML, JSON and email templates so it's become a templating engine for me as well as a view engine. This is made possible because it allows you to render views to strings rather, which isn't (easily) available in the standard view engine.

It's also important to note that you don't have to use a single view engine either. You can use multiple engines at the same time so you might use the default view engine for HTML templates, but switch to spark for XML.

Overall, if the default view engine is doing everything you need and you're happy with it then I wouldn't bother switching at all, however if you have specific needs that aren't being met by the default view engine then maybe it's time to look at alternatives.

Corriecorriedale answered 11/11, 2009 at 23:39 Comment(0)
P
1

Personally, I don't use alternate view engines, but their appeal is that of cleaner views. If you're fluent in Spark, that first example is a lot nicer and easier to read.

However, I prefer to wrap logic such as what you have given into a helper method. I don't have to learn something new, all of my coworkers can understand it, and my views stay relatively clean. It's a completely subjective matter, and either approach is fine.

These view engines are largely a carry-over from other frameworks like RoR and Django. Django's argument for its view templating system is that views should only be for view logic. Therefore, if you restrict what is possible in your view engine, you have fewer opportunities to mix up responsibilities between controllers and views.

Pyrophyllite answered 11/11, 2009 at 23:31 Comment(0)
U
1

I'd say it's more of an option than anything else. It's like deciding between C# and Visual Basic, use what you know best and what you will be more productive in.

Uke answered 12/11, 2009 at 0:3 Comment(0)
M
0

Some of benefits of Spark (what I like):

  1. You can use partial views like html tags. Example of use can be tag for rounded corners.
  2. You don't get "tag soup". Maybe you don't see benefit right now, but if you have huge views, it is much more readable.
  3. You get strongly typed access to ViewData["..."].
  4. You can easily render views to string.
  5. Automatically applies Html.Encode, increasing safety of your application.

What I don't like:

  1. Has issues with Intellisense and Resharper.
  2. Can't use Format document option.

I Spark had this issues solved, I would see no reason to use standard view engine.

Microscopium answered 11/11, 2009 at 23:58 Comment(5)
asp.net 4.0 has new encoding tags, which in MVC 2.0 can be used: <%: Model.Something %> that has Encoding ON by default. If you use David Ebbo's t4 templates, you get intellisense everywhere, including viewdata you can get a lot of code separation with partial viewsUke
I use VS2008 and ASP.NET 3.5 and I wan't to use tags and engines that are compatible with it.Microscopium
Have you tried to format document as HTML in any editor (e.g. Notepad++)? Or even rename to .html - format - rename to .spark? I think this should do since Spark is, well, kind of HTML ;-)Snatch
@queen3: I tried using XML editor in VS and formatting there. That didn't work well. C# code was poorly formatted and it is not very comfortable.Microscopium
Well, if you have # code this can be a problem (though it wasn't for me); but majority of my views do not have it. And I've just tried formatting as XML and HTML, they both do and produce clean, while slightly different, results. Now, I could also successfully format with Notepad++'s HtmlTidy options - which gives a lot of flexibility in config. It could also cope with # code without problems. And if you have THAT much C# code in your views then you better move it out elsewhere, anyway ;-)Snatch

© 2022 - 2024 — McMap. All rights reserved.