Example #1
0
        public static string RenderTemplate(string template, ViewDataDictionary viewData)
        {
            // Set up your spark engine goodness.
            var settings = new SparkSettings().SetPageBaseType(typeof(SparkView));

            var engine = new SparkViewEngine(settings)
            {
                ViewFolder = new FileSystemViewFolder(AppDomain.CurrentDomain.BaseDirectory + @"\..\..\..\IntegrationTestingViews\Views")
            };

            // "Describe" the view (the template, it is a template after all), and its details.
            var descriptor = new SparkViewDescriptor().AddTemplate(template);

            // Create a spark view engine instance
            var view = (SparkView)engine.CreateInstance(descriptor);

            try
            {
                // Merge the view data.
                viewData.Keys.ToList().ForEach(x => view.ViewData[x] = viewData[x]);

                // Render the view to a text writer.
                var writer = new StringWriter();
                view.RenderView(writer);
                return(writer.ToString());
            }
            finally
            {
                engine.ReleaseInstance(view);
            }
        }
Example #2
0
        public string Convert(string templateName, ViewData data, string relativeOutputPath)
        {
            string template = templateName;

            if (template.StartsWith(templatePath))
            {
                template = template.Substring(templatePath.Length + 1);
            }

            SparkViewDescriptor descriptor = new SparkViewDescriptor()
                                             .AddTemplate(template);
            var view = (SparkTemplateBase)engine.CreateInstance(descriptor);

            using (var writer = new StringWriter())
            {
                try
                {
                    view.RelativeOutputPath = relativeOutputPath;
                    view.ViewData           = data;
                    view.RenderView(writer);
                }
                finally
                {
                    engine.ReleaseInstance(view);
                }

                return(writer.ToString());
            }
        }
Example #3
0
        public void Render(Template template, object data, TextWriter output)
        {
            var descriptor = new SparkViewDescriptor().AddTemplate(template.Name + ".spark");

            var view = (ViewBase)_engine.CreateInstance(descriptor);

            try
            {
                view.Data = data;
                view.RenderView(output);
            }
            finally
            {
                _engine.ReleaseInstance(view);
            }
        }
Example #4
0
        public string Populate(string templateFilePath, object data)
        {
            var writer = new StringWriter();

            var descriptor = new SparkViewDescriptor();

            descriptor.AddTemplate(templateFilePath);
            var view = (TemplateBase)engine.CreateInstance(descriptor);

            try {
                view.ViewData = new ViewDataDictionary(data);
                view.RenderView(writer);
            } finally {
                engine.ReleaseInstance(view);
            }
            return(writer.ToString());
        }