Brail View Engine

The Brail view engine has been ported from MonoRail to work with the Microsoft ASP.NET MVC Framework. For an introduction to Brail, see the documentation on the Castle project website.

Configuration

In order to use Brail, you will need to reference the following DLLs in your project:
  • MvcContrib.dll
  • MvcContrib.BrailViewEngine.dll
  • Boo.Lang.dll
  • Boo.Lang.Compiler.dll
  • Boo.Lang.Extensions.dll
  • Boo.Lang.Parser.dll

Note that Brail uses a custom build of Boo that includes support for .NET 3.5 Extension Methods. These DLLs can be found in the Trunk at http://mvccontrib.googlecode.com/svn/trunk/bin/boo/

Next, you will need to configure Brail by adding the relevant sections to your web.config file:

<configuration>
	<configSections>
		<section name="brail" type="MvcContrib.BrailViewEngine.BrailConfigurationSection, MvcContrib.BrailViewEngine"/>
	</configSections>
	<brail debug="true" saveToDisk="false" batch="false" commonScriptsDirectory="CommonScripts" saveDirectory="CompiledViews">
		<reference assembly="MVCToolkit"/>
		<import namespace="System"/>
	</brail>
	... 
</configuration>


The Brail configuration section supports the following attributes:
  • debug="true|false" - Whether the Boo compiler should generate debuggable code.
  • saveToDisk="true|false" - Whether the compiled Brail scripts should be stored on disk or in memory.
  • saveDirectory="directory_name" - If saveToDisk is true, this is where the compiled scripts will be stored.
  • batch="true|false" - Whether all the Brail scripts should be compiled to a single assembly.
  • commonScriptsDirectory="directory_name" - Where to find any common code that should be accessible by all views.

You can also reference additional assemblies by adding <reference /> elements. By default, you will probably want to reference the MVCToolkit dll in order to make use of the HTML helper extension methods.

Adding <import /> elements will automatically add namespace imports to your scripts, removing the need to manually import the namespaces in your scripts. The System.Web.Mvc namespace will be automatically imported.

A Simple Example

With Brail configured, you will now need to set your controllers to use the BrailViewFactory rather than the default WebFormsViewFactory. The simplest way to do this is by setting the ViewFactory property on your controller.

namespace BrailTest1.Controllers
{
	using MvcContrib;
	using MvcContrib.ViewFactories;

	public class HomeController : ConventionController
	{
		public HomeController()
		{
			this.ViewFactory = new BrailViewFactory();
		}

		public void Index()
		{
			RenderView("Index");
		}
	}
}


When the Index action is executed, the view Home\Index.brail will be executed. A simple Brail script might look like this:

Hello, the time is ${DateTime.Now}


Visiting mysite.com/Home/Index would result in the following output:

Hello, the time is 26/01/2008 10:51:10


Rather than setting the ViewFactory property directly on your controller, a better approach may be to use an IoC container. See the documentation on Inversion of Control for information on instantiating controllers with an IoC container.

Layouts

Brail supports the concept of Layouts, which are the equivalent to ASP.NET Master Pages. To render a view using a particular layout, you can use the existing RenderView method:

public void Index() 
{
	RenderView("Index", "DefaultLayout");
}


Layouts should be stored in the Views\Layouts directory. A sample layout might look like this:

<html>
	<head>
		<title>My site</title>
	</head>
	<body>
		<h1>My sample site</h1>
		<div>
			${childOutput}
		</div>
	</body>
</html>


The contents of your view will be placed at the location of ${childOutput}.

Accessing Data in Views

Any items placed in the default ViewData dictionary will be available in your views:

In the action:
public void Index() 
{
	ViewData["project"] = "MvcContrib";
	RenderView("Index", "DefaultLayout");
}


In the View:
Project: ${project}


The same is also true for the TempData collection, the QueryString (Request.QueryString) and any Form data (Request.Form).

If you use the Typed ViewData approach, then you will need to use the viewData variable in your views:

In the Action:
public void Index() 
{
	Customer cust = new Customer();
	cust.Name = "John Smith";
	RenderView("Index", "DefaultLayout", cust);
}


In the View:
Viewing details for customer: ${viewData.Name}


Several other variables are automatically made available to views:

Variable Explanation
Controller The controller being executed.
Context The HTTP Context
request The current HTTP Request
response The current HTTP Response
session The session
html Helper used for generating HTML
url Helper used for resolving URLs
siteRoot The application root


The the "html" and "url" variables are instances of System.Web.Mvc.HtmlHelper and UrlHelper respectively. As such, you can use all of the MVCToolkit's extension methods as long as the appropriate reference is specified in the Brail configuration section. For example:

${Html.TextBox('projectName', project)}	

Last edited Jan 30, 2008 at 8:13 AM by JeremyS, version 2

Comments

SteveGentile Dec 23, 2008 at 3:26 AM 
Nevermind, I had to use lower case - the table above shows it right, but the sample show upper case Html.TextBox vs. html.TextBox. Side question...are there any intellisense tools for VS with .brail files by chance? :) Thanks again

SteveGentile Dec 23, 2008 at 3:24 AM 
I get this error: 'TextBox' is not a member of 'System.Web.Mvc.Html'. when using Html.TextBox - what am I missing? Thanks!

QLeelulu Dec 2, 2008 at 2:02 PM 
<%for element in list: output "<li>${element}</li>"%>
this code wouldn't work
must add the "end" at the new line, like this:
<%for element in list: output "<li>${element}</li>"
end%>

QLeelulu Dec 2, 2008 at 1:57 PM 
can this define a method? like this:
<%
[Html]
def HtmlMethod():
return "Some text that will be <html> encoded"
end
%>
${HtmlMethod()}

and can this use the OutputSubView methed? like this:
<?brail OutputSubView("/home/menu")?>