Michael edited this page Jun 28, 2016 · 2 revisions

Adding to Castle MonoRail

In the web application add references to the Castle.MonoRail.Views.Spark and Spark assemblies. In the <monorail> section configure the spark view engine.

  <viewEngines viewPathRoot="Views">
    <add type="Castle.MonoRail.Views.Spark.SparkViewFactory, Castle.MonoRail.Views.Spark"/>

That's about it really.

Adding to Asp.Net MVC

View engines are not managed by the controller.

The simplest way to use Spark from an Asp.Net MVC web application is to register an instance of the SparkViewFactory inn your Global Application_Start function at the same point you would be registering routes and such.

protected void Application_Start(object sender, EventArgs e)
    ViewEngines.Engines.Add(new SparkViewFactory());

This is also an excellent place to provide any other non-default services to the view factory. Such as an IViewFolder that reads from embedded resources instead of the file system, or an IViewActivatorFactory which can take over the responsibility of instantiating the compiled view types.

TODO - document use of IoC for this

Spark settings in config file

After the Spark view engine is integrated into your MVC, MonoRail, or other application there are some additional settings you may want to configure. One way to do this is with a <spark> config file section.

  <section name="spark" type="Spark.Configuration.SparkSectionHandler, Spark"/>
  <compilation debug="true|false" defaultLanguage="Default|CSharp|VisualBasic">
      <add assembly="YourAssemblyName" />
      <add assembly="YourOtherStrongAssemblyName, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
  <pages automaticEncoding="true|false" pageBaseType="Your.NonDefault.BaseSparkView" prefix="{optional string}">
      <add namespace="System"/>
      <add namespace="System.Collections.Generic"/>
      <add namespace="System.Linq"/>
      <add namespace="System.Web.Mvc"/>
      <add match="/content/css" location=""/>
      <add match="/content/js" location=""/>

The defaultLanguage may be specified if you are using a language like VisualBasic. It's not necessary for nearly any other case.

Additional assemblies and namespaces can be referenced with <use assembly="..." namespace="..." /> in view, layout, and partial files. All assemblies loaded in the app domain are automatically referenced, there may be times you want to add references explicitly for assemblies that aren't yet loaded.

The default page base type, MvcContrib.SparkViewEngine.SparkView or Castle.MonoRail.Views.Spark.SparkView, will be used if one isn't specified. If you do provide a class as the page base type you'll probably inherit from the default base class.

Automatic HTML encoding of output may be enabled. This will result in every instance of ${expr} output passing through the intrinsic HTML encoding method H(). The syntax !{expr} is used in this case for output which should be sent unencoded.

An optional prefix may be selected, which must then be used on all Spark elements and attributes in your template files. The prefix should not be set here if you're defining a prefix in the templates with xmlns attributes.

The resources section may be used to control the result of ~/ style urls. For example, <script src="~/content/js/foo.js"/> will normally appear as a ${SiteRoot} prefixed url but the match for "/content/js" above will make that url appear as the given location + "/foo.js". The assumption for high-volume sites is you would use the normal urls for development and map them to a static file server or to a cdn provider in production.

Spark settings in code

Another way to provide these settings to the spark engine is to provide an ISparkSettings instance to the constructor.

protected void Application_Start(object sender, EventArgs e)
    var settings = new SparkSettings()
    ViewEngines.Engines.Add(new SparkViewFactory(settings));
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.