Getting started
Brail includes many changes to the way that you normally write code in Boo. Brail views are scripts files that ends with "*.brail" or "*.brailjs". The most significant change is that Brail does not use whitespace to control blocks. This mean that the following statement in Boo:
if someCondition:
DoAction()
Will look like this in Brail:
if someCondition:
DoAction()
end
Blocks are controlled starting with a colon and ending by "end". While this is probably the most significant change from Boo, there are many other things that Brail does for you so you would get a scripting experience while working in a compiled langauge. Read on and discover what makes Brail so special.
Getting started with Brail
First of all you must reference the following assemblies. Copy them to the bin folder if you are not using Visual Studio.
- Castle.MonoRail.Views.Brail.dll
- Boo.Lang.dll
- Boo.Lang.Compiler.dll
- Boo.Lang.Parser.dll
- anrControls.Markdown.dll
The Boo.* files are required for the language support. The anrControl.Markdown.dll is required for support Markdown formatting.
Edit your web.config file to include the following line (in the monoRail config section, of course):
<viewEngine viewPathRoot="Views" customEngine="Castle.MonoRail.Views.Brail.BooViewEngine, Castle.MonoRail.Views.Brail" />
And that is it, you are now capable of using Boo scripts to write views in MonoRail! Now that you can use it, let's see what you can do with it.
Before we get to how to use it, I must send huge thanks to the Boo Community, for being so helpful.
Security
If your view directory is in the web directory, it's wise to not allow the files to be read through http. So under the system.web/httpHandlers configuration you should add the following:
<add verb="*" path="*.brail" type="System.Web.HttpForbiddenHandler"/> <add verb="*" path="*.brailjs" type="System.Web.HttpForbiddenHandler"/>
The mandatory 'hello world'
First, Brail scripts are not normal Boo programs, they ''require'' that you would have at least one statement at the global namespace, which is what MonoRail will end up calling when your view is invoked. Assuming that you've already have a controller, and that you've setup your environment correctly, you can simply do this:
Hello, World!
Brail will take this script, compile it and send its output to your browser. The nice thing about it is that if you would change it to say:
Hi, World!
You will instantly get a the updated view, this is highly important to development scenario.
This is a developer feature, meant to ease development. Using it on a production machine would cause recompiling of the scripts and cause an ''assembly leak'' until the application domain is restarted. On a developer machine, this should rarely be a problem, on a production machine, if frequent changes are made to the scripts, this can cause problems.
You can also use parameters that the controller has put in the Property Bag or Flash, like this:
Hi, ${User}!
If you are wondering how does it work behind the scenes: the script is loaded and compiled. There is some magic there that sends anything not wrapped in <% %> directly to the user (which will also expand ${arg} expression to their values). The compiled code is cached, so you pay the compilation cost only once.