Dashboard > MonoRail > Home > MonoRail Helper
MonoRail Helper
Added by Chorn Sokun, last edited by Chorn Sokun on Sep 05, 2007  (view change) show comment
Labels: 
(None)


Helpers

After get my first Hello World application up and running I began to dig into the framework. I end up in the Castle.MonoRail.Framework.Helpers where I find alot of interesting stuffs. I must admit that HtmlHelper had very useful xml comments; if all developers follow this good habit I can see a lot of benefit for the end user.

UrlHelper

I will show you how to properly dynamic generate link using UrlHelper class.

$Url.Link('Edit', "%{area='admin', controller='cutomer', action='edit', querystring='id=$customer.Id'}");

result: admin/customer/edit.rails?id=1

$Url.Link('Edit', "%{area='admin', controller='job', action='edit', querystring='id=$customer.Id&status=$customer.status'}");

result: admin/customer/edit.rails?id=1&status=true

HtmlHelper

DateTime fields are required ~78% of the time. I wanted to find out what is the best practice for this problem.
I found the following code fragment by searching the forum:

$Form.Select("User.BirthdateMonth", $User.Birthdate.Month, [1..12], "%{firstoption='MM'}")
$Form.Select("User.BirthdateDay", $User.Birthdate.Day, [1..31], "%{firstoption='DD'}")
$Form.Select("User.BirthdateYear", $User.Birthdate.Year, [1990..2007], "%{firstoption='YYYY'}")

And this segment of code after I looked into the Castle.MonoRail.Framework.Helpers.HtmlHelper class:

$Html.DateTime("User.Birthdate", %User.BirthDate)

Although I found it useful, but from the users point of view it is not intuitive. Why? The user has to select 3 boxes for just one DateTime entry - it would be better if I can have $Html.DateTime generate calendar base on jquery Calendar instead.

AbstractHelper

There are a few methods inside Castle.MonoRail.Framework.Helpers.AbstractHelper that I found very useful. Since is mark as Abstract you can not use this class directly. Anyway all the FormHelper, HtmlHelper ... are derived from AbstractHelper so you can use AbstractHelper method from these subclass for free. What I am going to show you here is one of the most frequently use method:

$Html.HtmlEncode("<script>alert('Hi');</script>");
Rule #2

Always escape your outputs.

Site running on a free Atlassian Confluence Community License granted to Castle Project. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.4 Build:#809 Jun 12, 2007) - Bug/feature request - Contact Administrators