Four View Engines for ASP.NET MVC

With last week’s introduction of Razor, there are now four major view engine for ASP.NET MVC. The others are Spark, NHaml, and the traditional ASPX file templates. This article introduces the four engines with a special focus on the new Razor engine.

The ASPX style view engine dates back to classic ASP. Place holders using the <%= %> or newer <%: %> syntax predominate this style. Over time ASPC controls were added and then master pages, but this also brought with it an expensive page life-cycle.

Spark, which is also usable in Castle Project’s MonoRail, takes a slightly different approach. While it does support placeholders, it also freely mixes HTML and code using special attributes and tags. While it can be somewhat verbose, it does have the advantage of being fully XML compliant.

NHaml is a .NET implementation of the Haml markup language. This language doesn’t use XML-like syntax at all. Using a sort of short-hand, it dispenses with a lot of the former’s syntactical noise such as end-tags.

The newest view engine if Microsoft’s Razor. Though all templating systems share some characteristics, this one is distinct from three view engines we have discussed so far. Unlike NHaml it still uses XML for markup, but unlike Spark it doesn’t go so far as to use XML for code. It isn’t exactly like ASPX either as it replaces the somewhat clumsy place holders with just the @ symbol followed by an expression or normal control block. No special end-marker is required, so the resulting code is quite terse.

By default all text from an @ expression is HTML-encoded. Text inside an @ block can contain “content blocks”. Identified by any XML tags, these switch you back to HTML mode much in the same way classic ASP developers used the %> html <%= trick. If you don’t actually want a tag to be emitted you can use <text> as a placeholder.

Since it uses existing VB or C# syntax, Microsoft expects Razor to be easy to learn. And while any text editor can be used, Visual Studio 2010 will be updated to offer full Intellisense for Razor files.

Another important feature for Razor is it’s compatibility with unit testing frameworks. Razor templates will not require a controller or web-server to host, so views written with it should be fully testable. While in theory anything can be tested, ASPX makes it rather difficult and the other two engines don’t really talk about testability in their respective web sites.

All four view engines support the concept of master pages, so there isn’t much to be said on this topic. Precompiling views into assembles is supported partially by ASPX and fully by Razor and Spark. NHaml currently has this on list of feature requests. What’s particularly interesting about Razor and Spark is that their precompiled views and be used for non-hosted scenarios like mail-merge engines.

Have you seen the NDjango view engine ? It is a port of Django templates to .NET written in F#. It also comes with Visual Studio extension including full blown Intellisense support. I would love to hear what you have to say about it.