コード例 #1
0
        public Action <TextWriter, object> Compile(TextReader template)
        {
            var configuration = CompiledConfiguration ?? new HandlebarsConfigurationAdapter(Configuration);

            using var reader = new ExtendedStringReader(template);
            return(HandlebarsCompiler.Compile(reader, configuration));
        }
コード例 #2
0
        public Func <object, string> CompileView(string templatePath)
        {
            var configuration   = new InternalHandlebarsConfiguration(Configuration);
            var createdFeatures = configuration.Features;

            for (var index = 0; index < createdFeatures.Count; index++)
            {
                createdFeatures[index].OnCompiling(configuration);
            }

            var compiler                 = new HandlebarsCompiler(configuration);
            var compiledView             = compiler.CompileView(templatePath, configuration);
            Func <object, string> action = (vm) =>
            {
                using (var writer = new PolledStringWriter(configuration.FormatProvider))
                {
                    compiledView(writer, vm);
                    return(writer.ToString());
                }
            };

            for (var index = 0; index < createdFeatures.Count; index++)
            {
                createdFeatures[index].CompilationCompleted();
            }

            return(action);
        }
コード例 #3
0
        private HandlebarsTemplate <TextWriter, object, object> CompileViewInternal(string templatePath, ViewReaderFactory readerFactoryFactory)
        {
            var configuration   = CompiledConfiguration ?? new HandlebarsConfigurationAdapter(Configuration);
            var createdFeatures = configuration.Features;

            for (var index = 0; index < createdFeatures.Count; index++)
            {
                createdFeatures[index].OnCompiling(configuration);
            }

            var compiledView = HandlebarsCompiler.CompileView(readerFactoryFactory, templatePath, configuration);

            for (var index = 0; index < createdFeatures.Count; index++)
            {
                createdFeatures[index].CompilationCompleted();
            }

            return((writer, context, data) =>
            {
                if (context is BindingContext bindingContext)
                {
                    var config = bindingContext.Configuration;
                    using var encodedTextWriter = new EncodedTextWriter(writer, config.TextEncoder, config.UnresolvedBindingFormat, config.NoEscape);
                    compiledView(encodedTextWriter, bindingContext);
                }
                else
                {
                    using var newBindingContext = BindingContext.Create(configuration, context, templatePath);
                    newBindingContext.SetDataObject(data);

                    using var encodedTextWriter = new EncodedTextWriter(writer, configuration.TextEncoder, configuration.UnresolvedBindingFormat, configuration.NoEscape);
                    compiledView(encodedTextWriter, newBindingContext);
                }
            });
        }
コード例 #4
0
        public HandlebarsTemplate <TextWriter, object, object> Compile(TextReader template)
        {
            using var container = AmbientContext.Use(_ambientContext);

            var configuration = CompiledConfiguration ?? new HandlebarsConfigurationAdapter(Configuration);

            var formatterProvider       = new FormatterProvider(configuration.FormatterProviders);
            var objectDescriptorFactory = new ObjectDescriptorFactory(configuration.ObjectDescriptorProviders);

            var localContext = AmbientContext.Create(
                _ambientContext,
                formatterProvider: formatterProvider,
                descriptorFactory: objectDescriptorFactory
                );

            using var localContainer = AmbientContext.Use(localContext);

            var compilationContext = new CompilationContext(configuration);

            using var reader = new ExtendedStringReader(template);
            var compiledTemplate = HandlebarsCompiler.Compile(reader, compilationContext);

            return((writer, context, data) =>
            {
                using var disposableContainer = AmbientContext.Use(localContext);

                if (writer is EncodedTextWriterWrapper encodedTextWriterWrapper)
                {
                    var encodedTextWriter = encodedTextWriterWrapper.UnderlyingWriter;
                    if (context is BindingContext bindingContext)
                    {
                        compiledTemplate(encodedTextWriter, bindingContext);
                        return;
                    }

                    using var newBindingContext = BindingContext.Create(configuration, context);
                    newBindingContext.SetDataObject(data);

                    compiledTemplate(encodedTextWriter, newBindingContext);
                }
                else
                {
                    if (context is BindingContext bindingContext)
                    {
                        var config = bindingContext.Configuration;
                        using var encodedTextWriter = new EncodedTextWriter(writer, config.TextEncoder, formatterProvider, config.NoEscape);
                        compiledTemplate(encodedTextWriter, bindingContext);
                    }
                    else
                    {
                        using var newBindingContext = BindingContext.Create(configuration, context);
                        newBindingContext.SetDataObject(data);

                        using var encodedTextWriter = new EncodedTextWriter(writer, configuration.TextEncoder, formatterProvider, configuration.NoEscape);
                        compiledTemplate(encodedTextWriter, newBindingContext);
                    }
                }
            });
        }
コード例 #5
0
        public void Process(BundleContext context, BundleResponse response)
        {
            if (!response.Files.Any())
            {
                return;
            }

            var compiler = new HandlebarsCompiler();

            var builder = new StringBuilder();

            builder.AppendFormat("var {0} = ", JsDictionaryName);
            builder.Append("{");

            var i = 0;

            foreach (var file in response.Files)
            {
                var virtualFilePath = String.Format("~{0}", file.VirtualFile.VirtualPath);
                var fileExtension   = virtualFilePath.Substring(virtualFilePath.LastIndexOf(".", StringComparison.OrdinalIgnoreCase));

                var templateKey = virtualFilePath
                                  .Replace("~", String.Empty)
                                  .Replace(fileExtension, String.Empty)
                                  .Replace(RootDirectory, String.Empty);

                var path     = context.HttpContext.Server.MapPath(virtualFilePath);
                var template = compiler.Precompile(File.ReadAllText(path));

                builder.AppendFormat("'{0}' : Handlebars.template({1})", templateKey, template);

                i++;

                if (i < response.Files.Count())
                {
                    builder.Append(",");
                }

                builder.Append(Environment.NewLine);
            }

            builder.Append("}");

            var content = builder.ToString();

            if (MinifyTemplates)
            {
                var minifier = new Minifier();
                var c        = minifier.MinifyJavaScript(content);
                if (minifier.ErrorList.Count <= 0)
                {
                    content = c;
                }
            }

            response.ContentType  = "text/javascript";
            response.Cacheability = HttpCacheability.Public;
            response.Content      = content;
        }
コード例 #6
0
 public Action <TextWriter, object> Compile(TextReader template)
 {
     using (var reader = new ExtendedStringReader(template))
     {
         var compiler = new HandlebarsCompiler(Configuration);
         return(compiler.Compile(reader));
     }
 }
コード例 #7
0
        private HandlebarsTemplate <TextWriter, object, object> CompileViewInternal(string templatePath, ViewReaderFactory readerFactoryFactory)
        {
            using var container = AmbientContext.Use(_ambientContext);

            var configuration = CompiledConfiguration ?? new HandlebarsConfigurationAdapter(Configuration);

            var formatterProvider       = new FormatterProvider(configuration.FormatterProviders);
            var objectDescriptorFactory = new ObjectDescriptorFactory(configuration.ObjectDescriptorProviders);

            var localContext = AmbientContext.Create(
                _ambientContext,
                formatterProvider: formatterProvider,
                descriptorFactory: objectDescriptorFactory
                );

            using var localContainer = AmbientContext.Use(localContext);

            var createdFeatures = configuration.Features;

            for (var index = 0; index < createdFeatures.Count; index++)
            {
                createdFeatures[index].OnCompiling(configuration);
            }

            var compilationContext = new CompilationContext(configuration);
            var compiledView       = HandlebarsCompiler.CompileView(readerFactoryFactory, templatePath, compilationContext);

            for (var index = 0; index < createdFeatures.Count; index++)
            {
                createdFeatures[index].CompilationCompleted();
            }

            return((writer, context, data) =>
            {
                using var disposableContainer = AmbientContext.Use(localContext);

                if (context is BindingContext bindingContext)
                {
                    bindingContext.Extensions["templatePath"] = templatePath;
                    var config = bindingContext.Configuration;
                    using var encodedTextWriter = new EncodedTextWriter(writer, config.TextEncoder, formatterProvider, config.NoEscape);
                    compiledView(encodedTextWriter, bindingContext);
                }
                else
                {
                    using var newBindingContext = BindingContext.Create(configuration, context);
                    newBindingContext.Extensions["templatePath"] = templatePath;
                    newBindingContext.SetDataObject(data);

                    using var encodedTextWriter = new EncodedTextWriter(writer, configuration.TextEncoder, formatterProvider, configuration.NoEscape);
                    compiledView(encodedTextWriter, newBindingContext);
                }
            });
        }
コード例 #8
0
            public HandlebarsEnvironment(HandlebarsConfiguration configuration)
            {
                if (configuration == null)
                {
                    throw new ArgumentNullException("configuration");
                }

                _configuration = configuration;
                _compiler      = new HandlebarsCompiler(_configuration);
                RegisterBuiltinHelpers();
            }
コード例 #9
0
        public HandlebarsTemplate <TextWriter, object, object> Compile(TextReader template)
        {
            var configuration      = CompiledConfiguration ?? new HandlebarsConfigurationAdapter(Configuration);
            var compilationContext = new CompilationContext(configuration);

            using var reader = new ExtendedStringReader(template);
            var compiledTemplate = HandlebarsCompiler.Compile(reader, compilationContext);

            return((writer, context, data) =>
            {
                if (writer is EncodedTextWriterWrapper encodedTextWriterWrapper)
                {
                    var encodedTextWriter = encodedTextWriterWrapper.UnderlyingWriter;
                    if (context is BindingContext bindingContext)
                    {
                        compiledTemplate(encodedTextWriter, bindingContext);
                        return;
                    }

                    using var newBindingContext = BindingContext.Create(configuration, context);
                    newBindingContext.SetDataObject(data);

                    compiledTemplate(encodedTextWriter, newBindingContext);
                }
                else
                {
                    if (context is BindingContext bindingContext)
                    {
                        var config = bindingContext.Configuration;
                        using var encodedTextWriter = new EncodedTextWriter(writer, config.TextEncoder, config.UnresolvedBindingFormatter, config.NoEscape);
                        compiledTemplate(encodedTextWriter, bindingContext);
                    }
                    else
                    {
                        using var newBindingContext = BindingContext.Create(configuration, context);
                        newBindingContext.SetDataObject(data);

                        using var encodedTextWriter = new EncodedTextWriter(writer, configuration.TextEncoder, configuration.UnresolvedBindingFormatter, configuration.NoEscape);
                        compiledTemplate(encodedTextWriter, newBindingContext);
                    }
                }
            });
        }
コード例 #10
0
        private Action <TextWriter, object> CompileViewInternal(string templatePath, ViewReaderFactory readerFactoryFactory)
        {
            var configuration   = CompiledConfiguration ?? new HandlebarsConfigurationAdapter(Configuration);
            var createdFeatures = configuration.Features;

            for (var index = 0; index < createdFeatures.Count; index++)
            {
                createdFeatures[index].OnCompiling(configuration);
            }

            var compiledView = HandlebarsCompiler.CompileView(readerFactoryFactory, templatePath, configuration);

            for (var index = 0; index < createdFeatures.Count; index++)
            {
                createdFeatures[index].CompilationCompleted();
            }

            return(compiledView);
        }