Home

Castle Stronghold

MonoRail Helpers

Helpers are associated with a controller and made available to be used on the view. They are usually used to reuse some generation code.

Creating a custom helper

A helper is just an ordinary class. It might optionally extend AbstractHelper in order to have access to the controller instance and some utility methods. For example:


public class MyHelper
{
    public String BuildUserLink(User user)
    {
        return String.Format("<a href='/users/showuser.rails?id={0}'>{1}</a>", 
            user.Id, user.Name);  
    }
}

The helper must be associate with the controller whose views might use it. This is done using the HelperAttribute:


using Castle.MonoRail.Framework;

[Helper(typeof(MyHelper))]
public class MemberController : Controller
{
    public void List()
    {
        PropertyBag.Add("users", ObtainUsers());
    }
}

Now it is just a matter of using the helper by its name:

 

#foreach ($user in $users)
    $MyHelper.BuildUserLink(${user})
#end

Builtin Helpers documentation

Google
Search WWW Search castleproject.org