Question

I use Spark View Engine with nested master pages. I have Application.spark which defines the basic layout of the website. Then there are several other masters which themselves use Application.spark as master page (Default.spark, SinlgeColumn.spark, Gallery.spark, ...)

If no master page is specified in a view file, then automatically Application.spark is choosen by the Spark View Engine. Since almost all my pages use "Default.spark" as master, is there a way to configure this globally?

The other possibilities would be:

  1. Set the master in each spark file individually <use master="Default" />. But that's really annoying.

  2. Rename my master files (Default.spark <-> Application.spark) but that really doesn't make any sense in naming.

Was it helpful?

Solution

There are two ways to solve your issue:

  1. Less work - but more repetitive.
  2. More work, but drives out your preferred convention.

Less Work

With any ActionResult, you can simply add a second parameter to the return statement to specify the name of the master to use:

return View("Gallery", "Default");

the first parameter name is the view and the second signifies a master page override...but that does mean you need to repeat it everywhere, and this isn't much better than what you had before.

More Work

The way that Spark locates the master page to use is via the following code in the framework:

protected virtual IEnumerable<string> PotentialDefaultMasterLocations(string controllerName, IDictionary<string, object> extra)
    {
        return ApplyFilters(new[]
                                {
                                    "Layouts\\" + controllerName + ".spark",
                                    "Shared\\" + controllerName + ".spark",
                                    "Layouts\\Application.spark",
                                    "Shared\\Application.spark"
                                }, extra);
    }

Notice the hardcoded Application.spark in there - that's a Spark convention. What it seems that you want to do is override this method and put something like this in instead:

protected virtual IEnumerable<string> PotentialDefaultMasterLocations(string controllerName, IDictionary<string, object> extra)
    {
        return ApplyFilters(new[]
                                {
                                    "Layouts\\" + controllerName + ".spark",
                                    "Shared\\" + controllerName + ".spark",
                                    "Layouts\\Default.spark",
                                    "Shared\\Default.spark"
                                    "Layouts\\Application.spark",
                                    "Shared\\Application.spark"
                                }, extra);
    }

Then it will find your Default.spark before it finds Application.spark or you could get rid of the Application.spark entirely if it doesn't ring true for you and you prefer your conventions...

In order to override this, all you need to do is create a class that inherits from Spark.Web.Mvc.DefaultDescriptorBuilder and override that method mentioned above and use it when you register the view engine like so:

public static void RegisterViewEngine(ICollection<IViewEngine> engines)
{
    var services = SparkEngineStarter.CreateContainer();
    services.SetServiceBuilder<IDescriptorBuilder>(
        c => new MyDescriptorBuilder());
    SparkEngineStarter.RegisterViewEngine(services);
}

This now means that you can now direct where Spark should look for the Master views and what the names will be.

Hope that helps,
All the best,
RobertTheGrey

OTHER TIPS

Don't know if it works, but try adding

<use master="Default" />

in a file called Views\_global.spark. It's automatically included in all views.

I usally add namespaces and global vars to my _global.spark.

Example:

<use namespace="System.Collections.Generic"/>
<use namespace="System.Web.Mvc.Html"/>

<global type="string" Title="'My default title'"/>

Another cool feature

Since you are a new spark user I would like to show you another cool spark feature.

Instead of writing

Html.LabelFor(Model => Model.FirstName);

you can write

<Label For="FirstName" />

Makes the views a bit cleaner, huh? The magic is done with something called bindings. All you need to do is to create a Views\Bindings.xml file and add bindings to it.

Read more in this blog entry: http://blog.robertgreyling.com/2010/08/spark-bindings-are-you-tired-of-eating.html

Instead of using Application.spark as your base, and Default.spark as your 2nd level... rename Default.spark to Application.spark and the application.spark to something else like BaseApplication.spark.

This way, by default your application.spark is picked up, but if you wish to override it you can referece the BaseApplication.spark.

See the three pass rendering section for an example. http://sparkviewengine.com/documentation/master-layouts#Threepassrendering

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top