Home > MonoRail > Castle Monorail – NVelocity View Engine

Castle Monorail – NVelocity View Engine

22 June, 2006

I’m using the recommended view engine NVelocity.

The following objects are available to the view code:
• context (IRailsEngineContext)
• request
• response
• session
• All entries in the request (if any)
• All entries in the Flash (if any)
• All entries in the PropertyBag (if any)

Also, a siteRoot string is added so you can build url definitions like:
<img src="$siteRoot/images/someimage.gif" />

Notation (variable name):

$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]


  • Normal notation: $mud-Slinger_9
  • Silent notation: $!mud-Slinger_9
  • Formal notation: ${mud-Slinger_9}

#foreach($i in $items)
#each (this is optional since its the default section)
text which appears for each item
text which appears before each item
text which appears after each item
text which appears between each two items
text which appears for every other item, including the first
text which appears for every other item, starting with the second
Content rendered if $items evaluated to null or empty
text which appears before the loop, only if there are items
matching condition
text which appears after the loop, only of there are items
matching condition

Binary expressions
#if($order.Status == "Undefined")
Sorry, but we don't know this order.
#elseif($order.Status == "Created")
Your order is being processed. Hold on!
#elseif($order.Status == "Dispatched")
Your order has been dispatched through UPS. Cross your fingers!

IDictonary Parameters
Calling a helper with IDictonary Paramaters:
$HtmlHelper.SubmitButton("Login", $DictHelper.CreateDict("id=btnLogin"))

Categories: MonoRail
  1. 22 June, 2006 at 1:48 pm

    As a matter of fact you can use the following to create dictionaries:

    $HtmlHelper.SubmitButton(“Login”, “%{id=’btnLogin’}”)

  2. 26 June, 2006 at 10:33 am

    That’s great thanks for that. It’s difficult to find the most up to date info on Monorail it seems as if the Wiki pages haven’t quite caught up to the source. If I remember correctly I actually got the $DictHelper.CreateDict from one of your posts on the mailing list.

    Do you have any tips on where to look for the latest information?

  3. 9 November, 2006 at 6:51 pm

    My question is where is $siteRoot created and stored. I would like to create a similar variable named $imageRoot that would be accessible to all controllers in an application just as $siteRoot is without having to use the ResourceAttribute. Forgive me if the answer is obvious, I am new to MonoRail.

  4. 10 November, 2006 at 10:52 am

    Robert you can have a Application controller which you then inherit from you other controllers:

    using System;
    using Castle.MonoRail.Framework;
    using Castle.MonoRail.ActiveRecordSupport;
    using PS.Helpers;

    namespace PS.Controllers
    [Helper(typeof(ViewHelper), “ViewHelper”)]
    public abstract class ApplicationController : ARSmartDispatcherController


    namespace PS.Controllers
    using System;
    using System.Collections.Generic;

    using Castle.ActiveRecord;
    using Castle.ActiveRecord.Framework;
    using Castle.MonoRail.Framework;
    using Castle.MonoRail.Framework.Helpers;

    using Models;

    public class PropertySearchController : ApplicationController

    You may also want to look at helpers if you need access in your views:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Castle.MonoRail.Framework.Helpers;
    using Uon.Web.Util;

    namespace PS.Helpers
    using System.Collections;
    using System.IO;

    class ViewHelper : AbstractHelper
    public string getHeader()

    Hope that helps. Stick with Monorail it’s a cracking framework!

  5. 10 November, 2006 at 3:09 pm

    An excellent suggestion. I have created a base controller class that has a view helper attribute that loads the imageRoot from the web.config file now. Thanks for your help.

  6. 11 January, 2007 at 5:42 pm

    Another question for you. How does one access the Flash from the view using NVelocity?

  7. Anonymous
    11 January, 2007 at 7:10 pm


    Flash[“error”] = “Oops an occurred!”;

    NVelocity view:


    Hope that helps!

  8. 2 August, 2007 at 5:52 pm



  1. No trackbacks yet.
Comments are closed.
%d bloggers like this: