Search code examples
asp.net-mvcconfigurationmaster-pagesspark-view-engine

Spark View Engine: How to set default master page name?


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.


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