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); } } }); }
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); } }); }
public DisposableContainer Configure() { return(AmbientContext.Use(_ambientContext)); }