Exemplo n.º 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));
        }
Exemplo n.º 2
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);
                    }
                }
            });
        }
Exemplo n.º 3
0
 public Action <TextWriter, object> Compile(TextReader template)
 {
     using (var reader = new ExtendedStringReader(template))
     {
         var compiler = new HandlebarsCompiler(Configuration);
         return(compiler.Compile(reader));
     }
 }
        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);
                    }
                }
            });
        }