internal static TemplateDelegate CompileView(ViewReaderFactory readerFactoryFactory, string templatePath, CompilationContext compilationContext) { var configuration = compilationContext.Configuration; IEnumerable <object> tokens; using (var sr = readerFactoryFactory(configuration, templatePath)) { using (var reader = new ExtendedStringReader(sr)) { tokens = Tokenizer.Tokenize(reader).ToArray(); } } var layoutToken = tokens.OfType <LayoutToken>().SingleOrDefault(); var expressions = ExpressionBuilder.ConvertTokensToExpressions(tokens, configuration); var compiledView = FunctionBuilder.Compile(expressions, compilationContext); if (layoutToken == null) { return(compiledView); } var fs = configuration.FileSystem; var layoutPath = fs.Closest(templatePath, layoutToken.Value + ".hbs"); if (layoutPath == null) { throw new InvalidOperationException($"Cannot find layout '{layoutToken.Value}' for template '{templatePath}'"); } var compiledLayout = CompileView(readerFactoryFactory, layoutPath, new CompilationContext(compilationContext)); return((in EncodedTextWriter writer, BindingContext context) => { var config = context.Configuration; using var bindingContext = BindingContext.Create(config, null); foreach (var pair in context.ContextDataObject) { switch (pair.Key.WellKnownVariable) { case WellKnownVariable.Parent: case WellKnownVariable.Root: continue; } bindingContext.ContextDataObject[pair.Key] = pair.Value; } using var innerWriter = ReusableStringWriter.Get(config.FormatProvider); using var textWriter = new EncodedTextWriter(innerWriter, config.TextEncoder, FormatterProvider.Current, true); compiledView(textWriter, context); var inner = innerWriter.ToString(); var viewModel = new LayoutViewModel(inner, context.Value); bindingContext.Value = viewModel; compiledLayout(writer, bindingContext); });
internal static TemplateDelegate CompileView(ViewReaderFactory readerFactoryFactory, string templatePath, CompilationContext compilationContext) { var configuration = compilationContext.Configuration; IEnumerable <object> tokens; using (var sr = readerFactoryFactory(configuration, templatePath)) { using (var reader = new ExtendedStringReader(sr)) { tokens = Tokenizer.Tokenize(reader).ToArray(); } } var layoutToken = tokens.OfType <LayoutToken>().SingleOrDefault(); var expressions = ExpressionBuilder.ConvertTokensToExpressions(tokens, configuration); var compiledView = FunctionBuilder.Compile(expressions, compilationContext); if (layoutToken == null) { return(compiledView); } var fs = configuration.FileSystem; var layoutPath = fs.Closest(templatePath, layoutToken.Value + ".hbs"); if (layoutPath == null) { throw new InvalidOperationException($"Cannot find layout '{layoutToken.Value}' for template '{templatePath}'"); } var compiledLayout = CompileView(readerFactoryFactory, layoutPath, new CompilationContext(compilationContext)); return((in EncodedTextWriter writer, BindingContext context) => { var config = context.Configuration; using var innerWriter = ReusableStringWriter.Get(config.FormatProvider); using var textWriter = new EncodedTextWriter(innerWriter, config.TextEncoder, config.UnresolvedBindingFormatter, true); compiledView(textWriter, context); var inner = innerWriter.ToString(); var vmContext = new [] { new { body = inner }, context.Value }; var viewModel = new DynamicViewModel(vmContext); using var bindingContext = BindingContext.Create(config, viewModel); compiledLayout(writer, bindingContext); });
internal static Action <TextWriter, object> CompileView(ViewReaderFactory readerFactoryFactory, string templatePath, ICompiledHandlebarsConfiguration configuration) { IEnumerable <object> tokens; using (var sr = readerFactoryFactory(configuration, templatePath)) { using (var reader = new ExtendedStringReader(sr)) { tokens = Tokenizer.Tokenize(reader).ToList(); } } var layoutToken = tokens.OfType <LayoutToken>().SingleOrDefault(); var expressionBuilder = new ExpressionBuilder(configuration); var expressions = expressionBuilder.ConvertTokensToExpressions(tokens); var compiledView = FunctionBuilder.Compile(expressions, configuration, templatePath); if (layoutToken == null) { return(compiledView); } var fs = configuration.FileSystem; var layoutPath = fs.Closest(templatePath, layoutToken.Value + ".hbs"); if (layoutPath == null) { throw new InvalidOperationException("Cannot find layout '" + layoutPath + "' for template '" + templatePath + "'"); } var compiledLayout = CompileView(readerFactoryFactory, layoutPath, configuration); return((tw, vm) => { string inner; using (var innerWriter = ReusableStringWriter.Get(configuration.FormatProvider)) { compiledView(innerWriter, vm); inner = innerWriter.ToString(); } compiledLayout(tw, new DynamicViewModel(new[] { new { body = inner }, vm })); }); }
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); }
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 HandlebarsTemplate <TextWriter, object, object> CompileView(string templatePath, ViewReaderFactory readerFactoryFactory) { readerFactoryFactory ??= ViewReaderFactory; return(CompileViewInternal(templatePath, readerFactoryFactory)); }
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 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) => { if (context is BindingContext bindingContext) { bindingContext.Extensions["templatePath"] = templatePath; var config = bindingContext.Configuration; using var encodedTextWriter = new EncodedTextWriter(writer, config.TextEncoder, config.UnresolvedBindingFormatter, 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, configuration.UnresolvedBindingFormatter, configuration.NoEscape); compiledView(encodedTextWriter, newBindingContext); } }); }
public static HandlebarsTemplate <TextWriter, object, object> CompileView(string templatePath, ViewReaderFactory readerFactoryFactory) { return(Instance.CompileView(templatePath, readerFactoryFactory)); }