Simplifying ASP.Net - NVelocity
NVelocity[1] is a .NET port of the Velocity open source template engine. I have used NVelocity quite a few times over the last couple of years.
- CSBTL - this project never went public, but basically it removed the need to understand web forums and complicated server controls for building Community Server blog themes.
- Graffiti - Graffiti supports a very simple theming model which enables building new themes with no knowledge of web forms and/or ASP.Net
While you have more options using aspx pages, I generally prefer the simplicity of doing web pages via NVelocity. There are no complicated server controls, complete control over the markup, simple extensibility, and no need to jump between contexts when iterating over a list.
With this in mind, I was very happy to find the NVelocityViewFactory as part of the MvcContrib project. With just a couple configuration steps, I was able to use NVelocity views for my ASP.Net MVC projects. Sweetness!
For example, Phil Haack, has a post which discusses options to doing a simple repeater. This could be greatly simplified with NVelocity (see below). No code needs to be written, no context switching for properties, etc.
<table>
#foreach($hobby in $hobbies)
<tr class="#if($velocityCount % 2 == 0)row #else alt-row #end">
<td>$hobby.Title</td>
</tr>
#end
</table>
In addition, using fancy loops we could easily add content to be shown if it is empty and better structure our markup.
#foreach($hobby in $hobbies)
#beforeall
<table>
#before
<tr class=
#even"row">
#odd
"alt-row">#each
<td>$hobby.Title</td>
#after
</tr>
#afterall
</table>
#nodata
<h3>Sorry, no hobbies</h3>
#end
The above example takes it to the extreme (all parts are optional), but should give you an indication of power available to you, again, all without requiring you to write a single line of code.
[1] As mentioned on the Castle site, the original NVelocity project seems to be dead. I have been using the updated version of Castle and recommend you use this version as well.
Similar Posts
-
.NET Visual Studio 2008 SP1 Beta VS2008 SP1 and .NET FX Beta Performance Improvements Introducing JScript
-
Announcements NetSquared Mashup Challenge - Developers Wanted! [Via: David Hurth ] ASP.NET Simplifying...
-
Link Listing - May 12, 2008
-
One of the major selling points of ASP.Net MVC is its extensibility options. Almost every piece of it can updated or swapped out. I have been hacking on the bits quite a bit over the last couple of weeks and have been using the MvcContrib to augment some
-
One of the major selling points of ASP.Net MVC is its extensibility options. Almost every piece of it can updated or swapped out. I have been hacking on the bits quite a bit over the last couple of weeks and have been using the MvcContrib to augment some
-
Pingback from Aut disce aut discede » Blog Archive » links for 2008-05-20
-
Pingback from SimpleTemplate : Simpable

Comments
Joe Brinkman on on 5.12.2008 at 6:08 PM
Declarative code is still code. Sticking a # on the front of foreach does not make it any less code.
Scott on on 5.12.2008 at 7:51 PM
I like the idea of an ASP.NET template language. But what does the looping syntax in NVelocity buy you over using the alligator tags?
Scott Watermasysk on on 5.12.2008 at 9:15 PM
@Scott
To me, it is just simpler and more light weight, especially when compared to the standard postback method on aspx pages.
Scott Watermasysk on on 5.12.2008 at 9:16 PM
@Joe
By code, I meant you do not have to write any code on the server. Take a look at the amount of code Phil had to write to do that simple loop.
I also find the lack of <%= %> everytime I want to access a property very helpful as well.
Scott Watermasysk on on 5.12.2008 at 9:26 PM
@Scott
One other thing. Using NVelocity, you have to do a lot less casting that you would in a regular aspx page (with or without MVC).
Some people may not like this lack of type of safety, but I personally enjoy it.
Joe Brinkman on on 5.13.2008 at 7:57 AM
@Scott - Velocity is definitely a well proven solution on the Java platform and NVelocity is a very capable port. My bigger problem is that there are so many template engines now vying for supremacy in the .Net space. It definitely adds to the challenges faced by developers trying to figure out where to spend their limited time to learn yet one more DSL.
david on on 6.04.2008 at 6:39 AM
Hey i have been trying to find examples of how to use NVelocity do you have do u have a any examples or source code that you would be willing to share at all i use asp.net 2.0 with linq if you have any examples of the two languages it would be greatly apreciated
God Bess