views:

110

answers:

2

Is is possible to use a master layout when using Spark in the form of the Direct Usage Sample? I have tried using both in my layout with my master layout in the same folder as the calling layout and I have also tried adding the master layout as a template to the SparkViewDescriptor in the code below?

    public class DefaultMessageBuilder : MessageBuilder 
    { 
        private readonly ISparkViewEngine _engine; 
        public DefaultMessageBuilder() 
        { 
            var settings = new SparkSettings() 
                .SetDebug(true) 
                .SetPageBaseType(typeof(TemplateBase)) 
                .AddNamespace("System.Collections.Generic"); 
            var templates = new VirtualPathProviderViewFolder("~/Templates"); 
            _engine = new SparkViewEngine(settings) { ViewFolder = templates }; 
        } 
        public override void Transform(string templateName, object data, TextWriter output) 
        { 
            var descriptor = new SparkViewDescriptor() 
                .AddTemplate("Master.spark") 
                .AddTemplate(templateName + ".spark"); 
            var view = (TemplateBase)_engine.CreateInstance(descriptor); 
            try 
            { 
                view.ViewData = new ViewDataDictionary(data); 
                view.RenderView(output); 
            } 
            finally 
            { 
                _engine.ReleaseInstance(view); 
            } 
        } 
    }
A: 

It look like for my particular situation where I want to have standard header and footer I can use file includes like this: <use file="~/Layouts/Master" />.

beckelmw
Did you determine that `<use master="">` wouldn't work? I want to use master layouts in a direct usage scenario as well, but I _really_ don't want to use the "sandwich" approach of separate header and footer templates...
Seth Petry-Johnson
+2  A: 

I was trying to achieve the same result and found that the order in which you add the views to the SparkViewDescriptor matters. Adding the view first and the master page last works fine for me.

With the following view descriptor it would use "viewPage1.spark" as the view and "masterPage1.spark" as the master page.

new SparkViewDescriptor().AddTemplate("viewPage1.spark").AddTemplate("viewPage.spark").AddTemplate("masterPage.spark").AddTemplate("masterPage1.spark") 

So it takes the first as the view and the last as the master page. Also it seems to neglect any <use master=""/> directives when you use it this way. I suppose there's some separate bit of code which reads the master directive and creates an appropriate SparkViewDescriptor for such a view when you're using it in the MVC scenario.

axk