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:

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:

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)}