Exemplo n.º 1
0
        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);
            });
        }
Exemplo n.º 2
0
 static bool HasLayout(MustacheConfiguration configuration)
 {
     return(File.Exists(
                Path.Combine(
                    configuration.TemplateRootPath,
                    String.Concat(configuration.LayoutTemplateName, configuration.TemplateFileExtension))));
 }
Exemplo n.º 3
0
        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);
                    });
                }
            }));
        }
Exemplo n.º 4
0
        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);
            };
        }
Exemplo n.º 5
0
        static bool HasLayout(MustacheConfiguration configuration)
        {
            var assembly     = Assembly.GetExecutingAssembly();
            var resourceName = MakeEmbeddedTemplateResourceName(configuration.LayoutTemplateName);

            return(assembly.GetManifestResourceNames().Contains(resourceName));
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        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));
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        static bool HasLayout(MustacheConfiguration configuration)
        {
            var assembly = Assembly.GetExecutingAssembly();
            var resourceName = MakeEmbeddedTemplateResourceName(configuration.LayoutTemplateName);

            return assembly.GetManifestResourceNames().Contains(resourceName);
        }
Exemplo n.º 10
0
        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;
        }
Exemplo n.º 11
0
        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);
                        });
                }
            });
        }
Exemplo n.º 12
0
 static bool HasLayout(MustacheConfiguration configuration)
 {
     return File.Exists(
         Path.Combine(
             configuration.TemplateRootPath,
             String.Concat(configuration.LayoutTemplateName, configuration.TemplateFileExtension)));
 }