Welcome Guest, you are in: Login

Castle Project


Navigation (MonoRail)

Search the wiki


View Engines

Modified on 2010/10/04 11:33 by Symon Rottem Categorized as Uncategorized

View engines are responsible for rendering the contents back to the browser. You can create your view engine by simply implementing the interface IViewEngine. For example, you can create an XML/XSL view engine, or WML, or whatever you can think of. Notice, however, that the view should be responsible for displaying logic and nothing more, nothing less.

By default the view engines will return pages with the MIME type text/html. If you would like to use application/xml+xhtml you can set this in your web.config file:

Castle.MonoRail.Framework.Views.NVelocity" />

If this attribute is set to true, and the user agent says it will accept application/xml+xhtml then the page will be returned using that MIME type instead. For browsers such as IE which do not understand the new MIME type, text/html will still be used.

Available View Engines

When working with MonoRail you can chose from a selection of different view engines, each with their own strengths and weaknesses. Some view engines are released and maintained but the folk who develop MonoRail, while others are provided by third parties.

Released with MonoRail

Maintained by Third Parties

View Engines Compared

WebFormsAny .net languageYesYesNo

Creating a View Engine

To implement your own view engine you need to implement the IViewEngine interface:

/// <summary>
/// Depicts the contract used by the engine
/// to process views, in an independent manner.
/// </summary>
public interface IViewEngine
	/// <summary>
	/// Evaluates whether the specified template exists.
	/// </summary>
	/// <returns><c>true</c> if it exists</returns>
	bool HasTemplate(String templateName);

	/// <summary>
	/// Processes the view - using the templateName 
	/// to obtain the correct template,
	/// and using the context to output the result.
	/// </summary>
	void Process(IRailsEngineContext context, Controller controller, String templateName);

	/// Processes the view - using the templateName 
	/// to obtain the correct template
	/// and writes the results to the System.TextWriter. 
	/// No layout is applied!
	/// </summary>
	void Process(TextWriter output, IRailsEngineContext context, Controller controller, String templateName);

	/// <summary>
	/// Wraps the specified content in the layout using 
	/// the context to output the result.
	/// </summary>
	void ProcessContents(IRailsEngineContext context, Controller controller, String contents);

The view engine implementation is also supposed to invoke a few hooks on the controller instance, namely PreSendView and PostSendView.

ScrewTurn Wiki version Some of the icons created by FamFamFam.