public static MiddlewareFunc Create( string templateRootDirectoryName = null, string templateFileExtension = null, string layoutTemplateName = null, Func <IDictionary <string, object>, object> layoutDataFunc = null) { templateRootDirectoryName = templateRootDirectoryName ?? "templates"; templateFileExtension = templateFileExtension ?? ".mustache"; layoutTemplateName = layoutTemplateName ?? "_layout"; var templateRootPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, templateRootDirectoryName); var configuration = new MustacheConfiguration { TemplateRootPath = templateRootPath, TemplateFileExtension = templateFileExtension, LayoutTemplateName = layoutTemplateName, LayoutDataFunc = layoutDataFunc }; return(next => environment => { environment[_confiugrationKey] = configuration; return next(environment); }); }
static bool HasLayout(MustacheConfiguration configuration) { return(File.Exists( Path.Combine( configuration.TemplateRootPath, String.Concat(configuration.LayoutTemplateName, configuration.TemplateFileExtension)))); }
static Task RenderTemplate( MustacheConfiguration configuration, Stream responseStream, Template template, object data, bool hasLayout, string bodyTemplateName = null) { return(Task.Run(() => { using (var writer = new StreamWriter(responseStream, Encoding.UTF8, 1, true)) { template.Render( data, writer, name => { if (hasLayout && name.Equals("body", StringComparison.OrdinalIgnoreCase)) { return GetTemplate(configuration, bodyTemplateName); } return GetTemplate(configuration, name); }); } })); }
public static MiddlewareFunc Create( string templateRootDirectoryName = null, string templateFileExtension = null, string layoutTemplateName = null, Func<IDictionary<string, object>, object> layoutDataFunc = null) { templateRootDirectoryName = templateRootDirectoryName ?? "templates"; templateFileExtension = templateFileExtension ?? ".mustache"; layoutTemplateName = layoutTemplateName ?? "_layout"; var templateRootPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, templateRootDirectoryName); var configuration = new MustacheConfiguration { TemplateRootPath = templateRootPath, TemplateFileExtension = templateFileExtension, LayoutTemplateName = layoutTemplateName, LayoutDataFunc = layoutDataFunc }; return next => environment => { environment[_confiugrationKey] = configuration; return next(environment); }; }
static bool HasLayout(MustacheConfiguration configuration) { var assembly = Assembly.GetExecutingAssembly(); var resourceName = MakeEmbeddedTemplateResourceName(configuration.LayoutTemplateName); return(assembly.GetManifestResourceNames().Contains(resourceName)); }
private static object GetLayoutData( MustacheConfiguration configuration, IDictionary<string, object> environment, object data) { if (configuration.LayoutDataFunc == null) return data; var templateData = data.ToDictionary(); var layoutData = configuration.LayoutDataFunc(environment).ToDictionary(); return new[] { templateData, layoutData } .SelectMany(x => x) .ToDictionary(pair => pair.Key, pair => pair.Value); }
private static object GetLayoutData( MustacheConfiguration configuration, IDictionary <string, object> environment, object data) { if (configuration.LayoutDataFunc == null) { return(data); } var templateData = data.ToDictionary(); var layoutData = configuration.LayoutDataFunc(environment).ToDictionary(); return(new[] { templateData, layoutData } .SelectMany(x => x) .ToDictionary(pair => pair.Key, pair => pair.Value)); }
static Template GetTemplate( MustacheConfiguration configuration, string templateName) { var templatePath = Path.Combine( configuration.TemplateRootPath, String.Concat(templateName, configuration.TemplateFileExtension)); if (!File.Exists(templatePath)) { throw new InvalidOperationException("Template path does not exist."); } var templateSource = File.ReadAllText(templatePath); var template = new Template(); template.Load(new StringReader(templateSource)); return(template); }
static bool HasLayout(MustacheConfiguration configuration) { var assembly = Assembly.GetExecutingAssembly(); var resourceName = MakeEmbeddedTemplateResourceName(configuration.LayoutTemplateName); return assembly.GetManifestResourceNames().Contains(resourceName); }
static Template GetTemplate( MustacheConfiguration configuration, string templateName) { var templatePath = Path.Combine( configuration.TemplateRootPath, String.Concat(templateName, configuration.TemplateFileExtension)); if (!File.Exists(templatePath)) throw new InvalidOperationException("Template path does not exist."); var templateSource = File.ReadAllText(templatePath); var template = new Template(); template.Load(new StringReader(templateSource)); return template; }
static Task RenderTemplate( MustacheConfiguration configuration, Stream responseStream, Template template, object data, bool hasLayout, string bodyTemplateName = null) { return Task.Run(() => { using (var writer = new StreamWriter(responseStream, Encoding.UTF8, 1, true)) { template.Render( data, writer, name => { if (hasLayout && name.Equals("body", StringComparison.OrdinalIgnoreCase)) return GetTemplate(configuration, bodyTemplateName); return GetTemplate(configuration, name); }); } }); }
static bool HasLayout(MustacheConfiguration configuration) { return File.Exists( Path.Combine( configuration.TemplateRootPath, String.Concat(configuration.LayoutTemplateName, configuration.TemplateFileExtension))); }