Home > Spark View Engine > Spark View Engine

Spark View Engine

One of the architecture beauty of ASP.NET MVC is that there are good building block and you can chop and change if you don’t like anything or want to try something different.So i thought I will give it a try and lets see how other view engines fit in is it really that easy to unplug one block and put another one ? Since razor is almost out I thought lets try developing something with an existing view engine other than the WebForms view engine.

I had heard a lot about the spark view engine. The other one which we all have heard is the nHaml view engine.

In order to get started with Spark View engine you have to download it from here.. Just unzip the files into a known folder and we are ready to rock-n-roll.

First we will create an ASP.NET MVC 2 project in visual studio and then add reference to Spark.dll and Spark.Web.Mvc.dll in your project

In order to configure the Spark view engine to be used in your MVC project you can either add configuration setttings it the web.config file or you can do it using the code. I am going to use the code approach to show you how easy it is to add the view engine.

So all you have to do is use the SparkViewFactory class and add it the ViewEngine collection of ASP.NET MVC and things can’t get better than this.

Next we will create a simple View Model object and call it PersonVm.I prefer the Vm convention at the end to indicate it is different than the actual model, its not a good idea to expose your domain model as views.

Now lets create a controller called PersonController and add some code to it’s Index action. Here we are just returning a collection of some PersonVm object.

Now lets create a new view for this controller by creating a folder called Person under views and then add a new text file and name it as Index.spark

Now lets write the code into the view so that the desired HTML can be rendered using the spark view engine. As you can see there a few different things in terms of syntax but overall the concept is the same. I really like the foreach construct and the convenience of the looping code.

Like webforms view engine Spark View engine also has a concept of master page but it is implemented in a slightly different manner.To create a master page you have to create a folder under views and call it Layouts and create a new text file and name it as Application.spark.

With this file you are telling the spark view engine is that this is the master page for all of spark views. I am sure if you look more into it there is a better way of implementing it but for a quick solution this way works.And now we add the following code to the master page.

As you can see when we specify use content=”view” markup in the spark application file the view engine knows this is where the content will be rendered. That’s it compile and hit F5 and viola your page is rendered with the desired results.

So the verdict is yes these blocks are easy to use and can plug and unplug them as per your desire, if you don’t like anything or want to try something different you can do it with out changing millions of line of XML file 🙂

  1. flightsrhodes
    July 21, 2010 at 11:39 pm

    ahhhhhh very good, bookmarked 🙂 keep it up, JusyKassy.

  2. Yogesh
    July 27, 2010 at 12:44 am

    What would be really helpful for someone to compare various engines for suitability to types of tasks. This will help someone like me who has not been using these engines in my daily routine.

    On another note: your blog framework may have a weird bug. If I want to leave a comment on a blog article already commented by someone before me, I don’t see a ‘Leave a comment’ link. Instead, I see a ‘n comment’ link. Perhaps it has something to do with the blog engine or the view engine(theme?).

    • July 28, 2010 at 5:02 am

      Yes there is definitely a problem with the comment section which disappears after the first comment. You are right it could be a theme problem and I will see if I can find some configuration which I can set to fix the problem.

      As far as view engine is concern I would suggest since you haven’t started using it then the best would be to go with “Razor” and i just blogged it today about it 🙂

  1. August 18, 2010 at 5:39 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: