public string RenderTemplate(string templateSource, object data) { var settings = new SparkSettings { PageBaseType = typeof(TestSparkView).Name }; settings.AddViewFolder(typeof (TestingViewFolder), new Dictionary<string, string> { {"templateSource", templateSource} }); settings.AddNamespace("OpenRasta.Codecs.Spark.Tests.TestObjects"); settings.AddNamespace("OpenRasta.Codecs.Spark.IntegrationTests"); settings.AddNamespace("OpenRasta.Codecs.Spark2.ViewHelpers"); settings.AddNamespace("System.Collections.Generic"); IWindsorContainer dependencies = CreateTestDependencies(); ISparkViewEngine sparkViewEngine = new SparkViewEngine(settings) { ExtensionFactory =dependencies.Resolve<ISparkExtensionFactory>() }; var descriptor = new SparkViewDescriptor(); descriptor.AddTemplate(TestingViewFolder.SingleTemplateName); var view = (TestSparkView)sparkViewEngine.CreateInstance(descriptor); view.Uris = new UriGenerator(DependencyManager.GetService<IUriResolver>()); view.ViewData = new TestViewData(data); return Render(view); }
public string Convert(string templateName, ViewData data) { 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.ViewData = data; view.RenderView(writer); } finally { engine.ReleaseInstance(view); } return writer.ToString(); } }
private static SparkViewDescriptor Desc(params string[] templates) { var desc = new SparkViewDescriptor(); foreach (string template in templates) desc.AddTemplate(template); return desc; }
public IBuilder Load(Options options) { var currentDirectory = Environment.CurrentDirectory; var assemblyDirectory = Path.GetDirectoryName(typeof(SakeEngine).Assembly.Location); var settings = new SparkSettings() .SetPageBaseType(typeof(BuilderBase)) .SetAutomaticEncoding(true) .SetAttributeBehaviour(AttributeBehaviour.TextOriented) .SetDebug(true); IViewFolder viewFolder = new FileSystemViewFolder(currentDirectory); foreach(var includeDir in options.IncludeDirectory) { viewFolder = new CombinedViewFolder(viewFolder, new FileSystemViewFolder(Path.Combine(currentDirectory, includeDir))); } viewFolder = new CombinedViewFolder(viewFolder, new FileSystemViewFolder(assemblyDirectory)); var engine = new SparkViewEngine(settings) { ViewFolder = viewFolder, ExtensionFactory = new ExtensionFactory(), }; var descriptor = new SparkViewDescriptor { Templates = new[] { options.Makefile } }; var builder = (BuilderBase)engine.CreateInstance(descriptor); builder.Output = new StringWriter(); builder.Log = _log; builder.Render(); return builder; }
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); } }
public string Render(string templateFile, ViewModel viewmodel) { string fName = Path.GetFileName(templateFile); string fDir = Path.GetDirectoryName(templateFile); var descriptor = new SparkViewDescriptor(); descriptor.AddTemplate(fName); descriptor.TargetNamespace = "templates"; this.engine.ViewFolder = new FileSystemViewFolder(fDir); var view = (MarkdownPageTemplate)this.engine.CreateInstance(descriptor); StringWriter writer = new StringWriter(); try { view.Model = viewmodel; view.RenderView(writer); return writer.ToString(); } finally { this.engine.ReleaseInstance(view); } }
public IList <SparkViewDescriptor> LoadBatchCompilation(Assembly assembly) { List <SparkViewDescriptor> list = new List <SparkViewDescriptor>(); foreach (Type type in assembly.GetExportedTypes()) { if (typeof(ISparkView).IsAssignableFrom(type)) { object[] customAttributes = type.GetCustomAttributes(typeof(SparkViewAttribute), false); if ((customAttributes != null) && (customAttributes.Length != 0)) { SparkViewDescriptor item = ((SparkViewAttribute)customAttributes[0]).BuildDescriptor(); CompiledViewEntry entry2 = new CompiledViewEntry { Descriptor = item, Loader = new ViewLoader() }; CSharpViewCompiler compiler = new CSharpViewCompiler { CompiledType = type }; entry2.Compiler = compiler; entry2.Activator = this.ViewActivatorFactory.Register(type); CompiledViewEntry entry = entry2; this.CompiledViewHolder.Store(entry); list.Add(item); } } } return(list); }
public ISparkViewEntry CreateEntryInternal(SparkViewDescriptor descriptor, bool compile) { var entry = new CompiledViewEntry { Descriptor = descriptor, Loader = CreateViewLoader(), Compiler = LanguageFactory.CreateViewCompiler(this, descriptor), LanguageFactory = LanguageFactory }; var chunksLoaded = new List <IList <Chunk> >(); var templatesLoaded = new List <string>(); LoadTemplates(entry.Loader, entry.Descriptor.Templates, chunksLoaded, templatesLoaded); if (compile) { entry.Compiler.CompileView(chunksLoaded, entry.Loader.GetEverythingLoaded()); entry.Activator = ViewActivatorFactory.Register(entry.Compiler.CompiledType); } else { entry.Compiler.GenerateSourceCode(chunksLoaded, entry.Loader.GetEverythingLoaded()); } return(entry); }
public virtual SparkViewDescriptor BuildDescriptor(IViewTemplate template, bool searchForMaster, IDictionary<string, object> extra, ICollection<string> searchedLocations) { var descriptor = new SparkViewDescriptor { TargetNamespace = GetNamespaceEncodedPathViewPath(template.GetFullPath()) }; descriptor.Templates.Add(template.GetFullPath().Replace("\\", "/")); if (searchForMaster && TrailingUseMasterName(descriptor) == null) { LocatePotentialTemplate( PotentialDefaultMasterLocations(template, extra), descriptor.Templates, null); } var trailingUseMaster = TrailingUseMasterName(descriptor); while (searchForMaster && !string.IsNullOrEmpty(trailingUseMaster)) { if (!LocatePotentialTemplate( PotentialMasterLocations(template, trailingUseMaster, extra), descriptor.Templates, searchedLocations)) { return null; } trailingUseMaster = TrailingUseMasterName(descriptor); } return descriptor; }
public virtual ViewCompiler CreateViewCompiler(ISparkViewEngine engine, SparkViewDescriptor descriptor) { var pageBaseType = engine.Settings.PageBaseType; if (string.IsNullOrEmpty(pageBaseType)) pageBaseType = engine.DefaultPageBaseType; var language = descriptor.Language; if (language == LanguageType.Default) language = engine.Settings.DefaultLanguage; ViewCompiler viewCompiler; switch (language) { case LanguageType.Default: case LanguageType.CSharp: viewCompiler = new CSharpViewCompiler(); break; case LanguageType.VisualBasic: viewCompiler = new VisualBasicViewCompiler(); break; case LanguageType.Javascript: viewCompiler = new JavascriptViewCompiler(); break; default: throw new CompilerException(string.Format("Unknown language type {0}", descriptor.Language)); } viewCompiler.BaseClass = pageBaseType; viewCompiler.Descriptor = descriptor; viewCompiler.Debug = engine.Settings.Debug; viewCompiler.NullBehaviour = engine.Settings.NullBehaviour; viewCompiler.UseAssemblies = engine.Settings.UseAssemblies; viewCompiler.UseNamespaces = engine.Settings.UseNamespaces; return viewCompiler; }
public SparkRenderer() { var factory = new SparkViewEngineFactory(); _engine = factory.CreateViewEngine(); _engine.LoadBatchCompilation(Assembly.Load(factory.TemplateAssembly)); _report = new SparkViewDescriptor().AddTemplate(SparkViewEngineFactory.ReportTemplate); _index = new SparkViewDescriptor().AddTemplate(SparkViewEngineFactory.IndexTemplate); }
private static SparkViewDescriptor createSparkDescriptor(bool useMaster, ViewDescriptor viewDescriptor) { var sparkDescriptor = new SparkViewDescriptor().AddTemplate(viewDescriptor.ViewPath); if (useMaster && viewDescriptor.Master != null) { appendMasterPage(sparkDescriptor, viewDescriptor.Master); } return sparkDescriptor; }
public virtual string CompileView(string view) { var viewFactory = new SparkViewFactory(); var descriptor = new SparkViewDescriptor {Language = LanguageType.Javascript}; descriptor.AddTemplate(string.Format("{0}{1}{2}", ControllerContext.ViewFolder, Path.DirectorySeparatorChar, view)); ((IViewSourceLoaderContainer)viewFactory).ViewSourceLoader = Context.Services.ViewSourceLoader; var entry = viewFactory.Engine.CreateEntry(descriptor); return entry.SourceCode; }
public ISparkViewEntry CreateEntry(SparkViewDescriptor descriptor) { var entry = CompiledViewHolder.Lookup(descriptor); if (entry == null) { entry = CreateEntryInternal(descriptor, true); CompiledViewHolder.Store(entry); } return(entry); }
public ISparkViewEntry Lookup(SparkViewDescriptor descriptor) { ISparkViewEntry entry; lock (_cache) { if (!_cache.TryGetValue(descriptor, out entry)) return null; } return entry.IsCurrent() ? entry : null; }
public void GenerateStrongUrls(UrlStrongModel strongModel, TextWriter writer) { var engine = new SparkViewEngineFactory().CreateViewEngine(); engine.LoadBatchCompilation(Assembly.Load("Machine.UrlStrong.Translation.Templates")); var descriptor = new SparkViewDescriptor().AddTemplate("master.spark"); var template = (TemplateBase)engine.CreateInstance(descriptor); template.Model = strongModel; template.RenderView(writer); }
public virtual SparkViewDescriptor BuildDescriptor(BuildDescriptorParams buildDescriptorParams, ICollection<string> searchedLocations) { var descriptor = new SparkViewDescriptor { TargetNamespace = GetNamespaceEncodedPathViewPath(buildDescriptorParams.ViewPath) }; if (!LocatePotentialTemplate( PotentialViewLocations(buildDescriptorParams.ViewPath, buildDescriptorParams.ViewName, buildDescriptorParams.Extra), descriptor.Templates, searchedLocations)) { return null; } if (!string.IsNullOrEmpty(buildDescriptorParams.MasterName)) { if (!LocatePotentialTemplate( PotentialMasterLocations(buildDescriptorParams.MasterName, buildDescriptorParams.Extra), descriptor.Templates, searchedLocations)) { return null; } } else if (buildDescriptorParams.FindDefaultMaster && TrailingUseMasterName(descriptor) == null /*empty is a valid value*/) { LocatePotentialTemplate( PotentialDefaultMasterLocations(buildDescriptorParams.ViewPath, buildDescriptorParams.Extra), descriptor.Templates, null); } var trailingUseMaster = TrailingUseMasterName(descriptor); while (buildDescriptorParams.FindDefaultMaster && !string.IsNullOrEmpty(trailingUseMaster)) { if (!LocatePotentialTemplate( PotentialMasterLocations(trailingUseMaster, buildDescriptorParams.Extra ), descriptor.Templates, searchedLocations)) { return null; } trailingUseMaster = TrailingUseMasterName(descriptor); } return descriptor; }
public PageGenerator(IResourceManager resourceManager) { var template = resourceManager.GetStringFromAssemblyOf<PageGenerator>("Forseti.Pages.Spark.Harness.spark"); var settings = new SparkSettings().SetPageBaseType(typeof(HarnessView)); var templates = new InMemoryViewFolder(); _engine = new SparkViewEngine(settings) { ViewFolder = templates }; templates.Add(TemplateName, template); _descriptor = new SparkViewDescriptor().AddTemplate(TemplateName); }
private static void appendMasterPage(SparkViewDescriptor descriptor, ITemplate template) { if (template == null) { return; } descriptor.AddTemplate(template.ViewPath); var viewDescriptor = template.Descriptor as ViewDescriptor; if (viewDescriptor != null) { appendMasterPage(descriptor, viewDescriptor.Master); } }
public ISparkViewEntry Lookup(SparkViewDescriptor descriptor) { ISparkViewEntry entry; lock (_cache) { if (!_cache.TryGetValue(descriptor, out entry)) { return(null); } } return(entry.IsCurrent() ? entry : null); }
protected override void Transform(string templateName, object data, TextWriter output) { var descriptor = new SparkViewDescriptor().AddTemplate(templateName + ".spark"); var view = (Spark.Web.Mvc.SparkView) _engine.CreateInstance(descriptor); try { view.ViewData = new ViewDataDictionary(data); view.RenderView(output); } finally { _engine.ReleaseInstance(view); } }
public PageGenerator(IResourceManager resourceManager, IFramework framework) { var template = resourceManager.GetStringFromAssemblyOf<PageGenerator>("Forseti.Pages.Spark.Harness.spark"); _framework = framework; var settings = new SparkSettings().SetPageBaseType(typeof(HarnessView)); var templates = new InMemoryViewFolder(); _engine = new SparkViewEngine(settings) { ViewFolder = templates }; templates.Add(TemplateName, template); //"<for each=\"var s in Stuff\"><p>${s}</p></for>"); _descriptor = new SparkViewDescriptor().AddTemplate(TemplateName); }
/// <summary> /// Called by handler classes to instantiate a view. /// </summary> /// <param name="templates"></param> /// <returns></returns> public BaseView CreateView(params string[] templates) { // the engine is created in Global.Application_Start var viewEngine = (ISparkViewEngine) Context.Application["ViewEngine"]; // a descriptor is used to "key" the type of view to instantiate var descriptor = new SparkViewDescriptor(); foreach (string template in templates) descriptor.AddTemplate(template); // create the view to return and provides the http context var view = (BaseView) viewEngine.CreateInstance(descriptor); view.Context = Context; return view; }
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); } }
public ISparkViewEntry GetViewEntry(SparkViewDescriptor descriptor) { ISparkViewEntry entry; var key = descriptor.GetHashCode(); _cache.TryGetValue(key, out entry); if (entry == null || !entry.IsCurrent()) { entry = _engine.CreateEntry(descriptor); lock (_cache) { _cache[key] = entry; } } return entry; }
private void RenderTemplate(IHttpEntity response, string templateAddress, object entity) { SparkViewDescriptor descriptor = new SparkViewDescriptor().AddTemplate(templateAddress); var engine = sparkConfiguration.Container.GetService<ISparkViewEngine>(); var view = (SparkResourceView) engine.CreateInstance(descriptor); view.ViewData = new ViewData(entity); view.Resolver = resolver; try { RenderToResponse(response, view); } finally { engine.ReleaseInstance(view); } }
public PageGenerator(IResourceManager resourceManager) { var template = resourceManager.GetStringFromAssemblyOf<PageGenerator>("Forseti.Pages.Spark.Harness.spark"); _jqueryJs = resourceManager.GetStringFromAssemblyOf<Forseti.Scripting.ScriptEngine>("Forseti.Scripting.Scripts.jquery.min.js"); _forsetiJs = resourceManager.GetStringFromAssemblyOf<Forseti.Scripting.ScriptEngine>("Forseti.Scripting.Scripts.forseti.js"); _requireJs = resourceManager.GetStringFromAssemblyOf<Forseti.Scripting.ScriptEngine>("Forseti.Scripting.Scripts.require.js"); _forsetiBootstrapperJs = resourceManager.GetStringFromAssemblyOf<Forseti.Scripting.ScriptEngine>("Forseti.Scripting.Scripts.forseti.bootstrapper.js"); var settings = new SparkSettings().SetPageBaseType(typeof(HarnessView)); var templates = new InMemoryViewFolder(); _engine = new SparkViewEngine(settings) { ViewFolder = templates }; templates.Add(TemplateName, template); _descriptor = new SparkViewDescriptor().AddTemplate(TemplateName); }
public override void Transform(string templateName, object data, TextWriter output) { // The view template var descriptor = new SparkViewDescriptor().AddTemplate(templateName + ".spark"); // The view that will contain view data //var view = (TemplateBase)_engine.CreateInstance(descriptor); //try //{ // view.ViewData = new ViewDataDictionary(data); // view.RenderView(output); //} //finally //{ // _engine.ReleaseInstance(view); //} }
public virtual ViewCompiler CreateViewCompiler(ISparkViewEngine engine, SparkViewDescriptor descriptor) { var pageBaseType = engine.Settings.PageBaseType; if (string.IsNullOrEmpty(pageBaseType)) { pageBaseType = engine.DefaultPageBaseType; } var language = descriptor.Language; if (language == LanguageType.Default) { language = engine.Settings.DefaultLanguage; } ViewCompiler viewCompiler; switch (language) { case LanguageType.Default: case LanguageType.CSharp: viewCompiler = new CSharpViewCompiler(); break; case LanguageType.VisualBasic: viewCompiler = new VisualBasicViewCompiler(); break; case LanguageType.Javascript: viewCompiler = new JavascriptViewCompiler(); break; default: throw new CompilerException(string.Format("Unknown language type {0}", descriptor.Language)); } viewCompiler.BaseClass = pageBaseType; viewCompiler.Descriptor = descriptor; viewCompiler.Debug = engine.Settings.Debug; viewCompiler.NullBehaviour = engine.Settings.NullBehaviour; viewCompiler.UseAssemblies = engine.Settings.UseAssemblies; viewCompiler.UseNamespaces = engine.Settings.UseNamespaces; return(viewCompiler); }
public virtual SparkViewDescriptor BuildDescriptor( DescriptorParameters descriptorParameters, ICollection<string> searchedLocations) { var descriptor = new SparkViewDescriptor { TargetNamespace = descriptorParameters.ViewPath }; IEnumerable<string> potentialLocations = PotentialViewLocations( descriptorParameters.ViewPath, descriptorParameters.ViewName, descriptorParameters.Extra); if (!LocatePotentialTemplate(potentialLocations, descriptor.Templates, searchedLocations)) { return null; } if (!string.IsNullOrEmpty(descriptorParameters.MasterName)) { potentialLocations = PotentialMasterLocations( descriptorParameters.MasterName, descriptorParameters.Extra); if (!LocatePotentialTemplate(potentialLocations, descriptor.Templates, searchedLocations)) { return null; } } else if (descriptorParameters.FindDefaultMaster && TrailingUseMasterName(descriptor) == null /*empty is a valid value*/) { potentialLocations = PotentialDefaultMasterLocations( descriptorParameters.ViewPath, descriptorParameters.Extra); LocatePotentialTemplate(potentialLocations, descriptor.Templates, null); } var trailingUseMaster = TrailingUseMasterName(descriptor); while (descriptorParameters.FindDefaultMaster && !string.IsNullOrEmpty(trailingUseMaster)) { if (!LocatePotentialTemplate(PotentialMasterLocations(trailingUseMaster, descriptorParameters.Extra), descriptor.Templates, searchedLocations)) { return null; } trailingUseMaster = TrailingUseMasterName(descriptor); } return descriptor; }
public static void RenderSparkView(this ManosModule module, IManosContext ctx, string template, object data) { if (engine == null) SetupEngine (); var descriptor = new SparkViewDescriptor ().AddTemplate (template).AddTemplate ("base.spark"); var view = (ManosSparkTemplate) engine.CreateInstance (descriptor); view.Data = data; try { view.RenderView (ctx.Response.Writer); ctx.Response.Writer.Flush (); } catch (Exception e) { Console.WriteLine ("Exception while writing template."); Console.WriteLine (e); } finally { engine.ReleaseInstance (view); } }
public override bool Equals(object obj) { SparkViewDescriptor descriptor = obj as SparkViewDescriptor; if ((descriptor == null) || (base.GetType() != descriptor.GetType())) { return(false); } if ((!string.Equals(this.TargetNamespace ?? "", descriptor.TargetNamespace ?? "") || (this.Language != descriptor.Language)) || (this.Templates.Count != descriptor.Templates.Count)) { return(false); } for (int i = 0; i != this.Templates.Count; i++) { if (!string.Equals(this.Templates[i], descriptor.Templates[i], StringComparison.InvariantCultureIgnoreCase)) { return(false); } } return(true); }
private static void Stub(FileSystemInfo source, FileSystemInfo destination) { if (source.FullName.EndsWith(".spark")) { // At this time the destination is already copied and processed by // 'simple' text substitution, so destination should be processed. var settings = new SparkSettings(); settings.AddNamespace("System"); var engine = new SparkViewEngine(settings) {DefaultPageBaseType = typeof(Template).FullName}; var folder = new InMemoryViewFolder {{source.Name, File.ReadAllText(destination.FullName)}}; engine.ViewFolder = folder; var descriptor = new SparkViewDescriptor(); descriptor.AddTemplate(source.Name); var view = (Template) engine.CreateInstance(descriptor); var builder = new StringBuilder(); using (var output = new StringWriter(builder)) { view.RenderView(output); File.WriteAllText(destination.FullName, output.ToString()); } engine.ReleaseInstance(view); } else if (source.FullName.EndsWith(".tt")) { var generator = new TemplateGenerator(); if (!generator.ProcessTemplate(source.FullName, destination.FullName)) { var exception = new TemplateException(source.FullName); foreach (CompilerError error in generator.Errors) { exception.AddError(error.ErrorText); } throw exception; } } }
public Stream Process(string virtualPath, string rootPath, Stream source, Dictionary<string, string> settings) { IViewFolder currentFolder = new Spark.FileSystem.CombinedViewFolder( new StringViewFolderFile(virtualPath, source), new Spark.FileSystem.FileSystemViewFolder(rootPath) ); /* foreach(var dir in Directory.GetDirectories(rootPath, "*", SearchOption.AllDirectories)) { currentFolder = new Spark.FileSystem.CombinedViewFolder( currentFolder, new Spark.FileSystem.FileSystemViewFolder(dir) ); } */ SparkViewEngine _engine = new SparkViewEngine(new Spark.SparkSettings()) { DefaultPageBaseType = typeof(ViewTemplate).FullName, ViewFolder = currentFolder, TemplateLocator = new MasterFileLocator(virtualPath, rootPath), }; var descriptor = new SparkViewDescriptor() .AddTemplate(virtualPath.TrimStart('\\')); var view = (ViewTemplate)_engine.CreateInstance(descriptor); view.RootPath = rootPath; view.VirtualPath = virtualPath; source.Dispose(); var tw = new StringWriter(); view.RenderView(tw); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(tw.ToString().Replace("@/", view.AppRelativeFolder))); return ms; }
public ViewDefinition(SparkViewDescriptor viewDescriptor, SparkViewDescriptor partialDescriptor) { ViewDescriptor = viewDescriptor; PartialDescriptor = partialDescriptor; }
public ISparkViewEntry GetEntry(SparkViewDescriptor descriptor) { return CompiledViewHolder.Current.Lookup(descriptor); }
public ISparkViewEntry GetEntry(SparkViewDescriptor descriptor) { return(CompiledViewHolder.Lookup(descriptor)); }
public ISparkView CreateInstance(SparkViewDescriptor descriptor) { return(CreateEntry(descriptor).CreateInstance()); }