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 })); }); }
internal sealed override object ReturnInvoke(BindingContext bindingContext, object context, object[] arguments) { using var writer = ReusableStringWriter.Get(bindingContext.Configuration.FormatProvider); WriteInvoke(bindingContext, writer, context, arguments); return(writer.ToString()); }