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.

Comments

Michael Ceranski , on 12/17/2009 2:29:04 PM Said:

Michael Ceranski

jonf - I agree you could remove some of the code in my example. However, I was trying to illustrate my point based on the example I found on Sparks website.

Gary - In order to get around the angle bracket issue there are a lot of new HTML tags and attributes introduced. I am not crazy about the fact that variable names are introduced in strings. It could definately be a problem if you are trying to debug a complex page and the compiler is not smart enough to parse those "string" variables.

In either case, I do admire the flow and clean look of the Spark example. I am not saying that Microsoft should adopt it 100% as is. However, it is a very interesting alternative that does have some practical uses.

Matt Brewer , on 12/17/2009 2:29:04 PM Said:

Matt Brewer

The main developer on the Spark project, Louis DeJardin, is now in the employ of Microsoft as part of the MVC team, if that lends anything to your idea.

jonf , on 12/17/2009 2:29:04 PM Said:

jonf

I think the first example is a bit unfair/convoluted. You don't need (or want) the open/close tags on every single line, so you can easily remove half of them in that example. Or, if you change the paragraph printout to use Response.Write (which for just one line of output is fine), you only need two of them. But sure, for larger blocks of HTML that can get ugly, especially due to the lack of HEREDOC support in ASP.NET.

Garry Pilkington , on 12/17/2009 2:29:04 PM Said:

Garry Pilkington

It looks like it introduces a lot of magic strings such as -var name in names-. Is this the case?
I do like the way it gets rid of the angle brackets though.

Michael Ceranski , on 12/17/2009 2:29:04 PM Said:

Michael Ceranski

Thanks for the update Matt! That is great news!

Microsoft tends to be pretty mindful of the opensource community these days. Sites like codeplex confirm this fact.

When you think about it, most great ideas come from opensource projects. People who are really passionate about coding and are willing to spend thier own free time developing something for others to use. I wish Louis the best at Microsoft. I hope they are compensating him well and giving him the influence he needs to improve the current MVC stack.

Ryan Hartzog , on 12/17/2009 2:29:04 PM Said:

Ryan Hartzog

"Then they will take some of that Spark goodness and integrate into their code base."

Or maybe Microsoft would find value in having an employee contribute to the project so that the project continues to stay active and lively? I would personally rather see that scenario instead of them trying to duplicate work of something already in existence and obviously valuable to the community.  I agree with the apprehension though.

Hirephpdevelopers United States, on 1/13/2010 12:56:33 AM Said:

Hirephpdevelopers

Here given nice details about open source ASP.NET MVC ViewEngine.Code detail containing about spark coding asp.net mvc.

SEO Services United States, on 1/22/2010 9:40:56 AM Said:

SEO Services

Pretty much any component can be replaced if it ... the Web Forms view engine for Nerd Dinner with an alternative.

Search Engine Optimization Service United States, on 4/3/2010 4:29:33 AM Said:

Search Engine Optimization Service

a for loop. The standard ASP.NET markup can make it hard to match up the beginning and ending brackets in nested code blocks.

Code Capers , on 5/16/2010 10:00:42 PM Said:

trackback

How to Build a Custom View Engine with Theme Support

How to Build a Custom View Engine with Theme Support

Comments are closed