web 2.0

MVC - Spark, The Alternative View Engine

UPDATE 1/8/2009 - Hmm...someone from Microsoft was paying attention to Spark..Maybe they read my blog?

Scott Hanselman chats with new Microsoft employee Louis Dejardin and ASP.NET team member about his open source ASP.NET MVC ViewEngine called "Spark." It's a totally new DSL (Domain Specific Language) that might make your MVC Views more fun to write!

Running time: 0h34m File size: 24.00MB

Download Original File | View original post


One thing I always disliked about ASP.NET was the amount of angle brackets (<%'s) that I had to sprinkle all over my HTML. Especially when you are dealing with if statements that conditionally modify the HTML output. Wouldn't it be nice if you could achieve the same results without all the angle brackets and percent signs?

Fortunately there are alternative View Engines out there. One alternative view engine that has been getting a lot of buzz in the MVC community lately is Spark. According to their website, Spark is a view engine for ASP.Net MVC and Castle Project MonoRail frameworks. The idea is to allow the html to dominate the flow and the code to fit seamlessly. If you take the time to look at their documentation page you will probably agree with me on the fact that they have achieved their initiative.

Here is a simple example of how Spark differs from a traditional ASP.NET markup syntax:

   1: <% var names="new [] {'alpha', 'beta', 'gamma'}" %>
   2:  <% foreach( var name in names ) { %>
   3:      <% if( name == "beta" ) { %>
   4:         <p>beta is my favorite</p>
   5:      <% } else { %>
   6:         <p><%=name%> is okay too I suppose.
   7:      <% } %>
   8:  <% } >

Now, Here is the same code written with Spark

   1:  <var names="new [] {'alpha', 'beta', 'gamma'}"/>
   2:  <for each="var name in names">
   3:    <test if="name == 'beta'">
   4:      <p>beta is my favorite.</p>
   5:      <else/>
   6:      <p>${name} is okay too I suppose. 
   7:    </test>
   8:  </for>

If you ask me, the developers who created Spark are definitely on to something here. Since we can use tags instead of angle brackets it is easier to write and maintain the HTML. Especially when it comes to situations where you have a if statement nested inside a for loop. The standard ASP.NET markup can make it hard to match up the beginning and ending brackets in nested code blocks.

For me personally, I tend to shy away from using third party tools unless it is absolutely necessary. There is nothing worse then writing a large application based on a third party solution only to find out that the project died, is no longer supported or riddled with bugs that will never be fixed. Therefore, you probably won't find me replacing the standard view engine in my MVC apps anytime soon. Don't get me wrong, I am not saying that the Spark project will die or that you should be discouraged from using it. I just want to warn you that there are advantages and disadvantages from swaying from the norm.

In a perfect world, an employee from the Microsoft ASP.NET team will look at the Spark view engine and have an epiphany. Then they will take some of that Spark goodness and integrate into their code base. Until then I will continue to deal with the angle brackets until I have a better reason to make the transition.

blog comments powered by Disqus