Moving from NVelocity to Brail

I'm a big fan of Castle's Monorail -- an MVC implementation for ASP.NET.  One of the nice things about it is that you get to pick which view engine you want to use.  The engines currently available are NVelocity, Brail (based on Boo) and AspView.  There's also a new view engine for Monorail based on NHaml.

When people first start learning Monorail, a lot of them use NVelocity.  It's a very simple templating language (which kind of forces you to keep complex logic out of the view) and most all of the Monorail documentation has samples using NVelocity.  I've used NVelocity for the few small Monorail projects I've done and I've been pretty happy with it.

I wanted to give Brail a try so I pulled out one of my older Monorail sample projects (doesn't everybody build a small sample app when they're first learning something?).  Monorail supports registering more than one view engine so I just updated my web.config to include brail as well.  This seemed like an easy way to run NVelocity and Brail side-by-side so I could play around with converting my old views to Brail as well as creating new ones.

I did notice one thing right away: You can't have a layout in one engine and the view in another.  My controller had a "default" layout (called default.vm).  I added a new method to the controller called "BrailTest" and then created my view "BrailTest.brail".  When Monorail found the "BrailTest.brail", that meant the layout had to be called "default.brail".  When the layout "default.brail" wasn't found, I got an error.  So even though it's technically possible to use two view engines at the same time, note that you'll have to maintain two layout files.  I don't even know what would happend with view components!  :)

Starting Fresh With Brail

So I decided to start a new Monorail project that would use Brail as the view engine.  Accessing my PropertyBag variables is almost exactly the same: $myVar in NVelocity vs. ${myVar} in Brail.  What I fell in love with his having a real .NET CLR language (Boo) for my view engine.  If I need some tricky logic, I just create a small helper method in Boo and I'm all set.  Very nice!

So here's a few points for anyone thinking about moving from NVelocity to Brail:

  1. Brail is case sensitive.  As a C# programmer, I usually keep my "case" in order, but I got sloppy with NVelocity and used a lot of lowercase everywhere.  Can't do that with Brail anymore.
  2. Layouts: While NVelocity uses "$childContent" to indicate the location of the rendered view, Brail uses "${ChildOutput}".
  3. If you decide to create a function in Brail ("def"), it needs to be the first thing in the Brail file -- before any rendering code.
  4. Boo uses indenting for blocks.  Brail adds an additional requirement of having an "end" keyword.  Not a big deal for me since I haven't done any real coding in Boo.
  5. Make sure you include the ":" with your else statements!  This one had me puzzled for quite a while.  If you're not familiar with blocks in Boo/Brail, he's a sample if statement:

if charity.AssignedRoom is not null:
    // do some stuff
end

Note the "end" is specific to Brail (not Boo).  Well, if you need to add an else clause, make sure you include the ":" with the else:

if charity.AssignedRoom is not null:
    // do some stuff
else:
    // do some other stuff
end

All in all, I really like Brail and don't think I'll be moving back to NVelocity any time soon.  Well done Ayende!

No Comments