Archive

Archive for the ‘Spark View Engine’ Category

Spark View Engine

July 21, 2010 4 comments

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 🙂

Advertisements