Configuring Spark

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.

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

That's about it really.

Adding to Asp.Net MVC

View engines are no managed by the controller. This section is now correct for Preview 5

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.

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

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()
        .SetDebug(true)
        .SetPageBaseType("Your.NonDefault.BaseSparkView")
        .AddAssembly("YourAssembly")
        .AddNamespace("System")
        .AddNamespace("System.Collections.Generic")
        .AddNamespace("System.Linq")
        .AddNamespace("System.Web.Mvc");
 
    ViewEngines.Engines.Add(new SparkViewFactory(settings));
}