Layouts
Layouts allow you to template your site by specifying common html and controls, such as structural html and navigation controls, in one place that are available for any view to use.
Layouts are just standard views, but they need to be created in a folder named layouts, notice that it is plural. The layouts folder needs to be directly under your views root directory.
Note that the extension for the layout files need to match whatever view engine you are using, such as .aspx. ASP.NET users that are tempted to use the master page model and use .master will be sadly dissappointed with a 'resource cannot be found' error.
You can associate a layout with a controller or with an action using the LayoutAttribute. For example:
using Castle.MonoRail.Framework; [Layout("application")] public class CustomerController : Controller { public void Index() { } }
In some scenarios you might want to turn off the layout processing. To do so use CancelLayout method. There are other cases where you want to render a specific view and turn off layout at the same time. The RenderView and RenderSharedView have overloads to allow you to do that.
| Method |
|---|
| RenderView(String name, bool skipLayout) |
| RenderView(String controller, String name, bool skipLayout) |
| RenderSharedView(String name, bool skipLayout) |
For example:
using Castle.MonoRail.Framework; [Layout("application")] public class CustomerController : Controller { public void Index() { RenderView("welcome", true); } }
Each view engine uses a specific approach to have layouts working. We will instruct on how to deal with layouts on the view engines documents.