Welcome Guest, you are in: Login

Castle Project

RSS RSS

Navigation (MonoRail)





Search the wiki
»

PoweredBy

Helpers

RSS
Modified on 2010/10/20 09:44 by Symon Rottem Categorized as Uncategorized

Introduction

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

Built-In Helpers

MonoRail comes packaged with several built in helpers to simplify development; please follow the links below for details on these helpers.

HelperDescription
FormHelperGenerate data bound form elements.
UrlHelperEasily build URL's and HTML anchors in your views.
AjaxHelperProvices AJAX support using the prototype jslib.
PaginationHelperSimplifies the creation of paginated navigation on items.
WizardHelperUsed in combination with MonoRail's Wizard support to create dynamic wizard navigation.
TextHelperProvides methods for working with strings and grammar.
Effects2HelperExposes script.aculo.us script features.
DateFormatHelperFormats DateTime instances.

Creating Custom Helpers

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 associated 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

ScrewTurn Wiki version 3.0.4.560. Some of the icons created by FamFamFam.