void ITemplate.Run(ExecuteContext context, TextWriter reader) #endif { _context = context; using (var memory = new MemoryStream()) { using (var writer = new StreamWriter(memory)) { _context.CurrentWriter = writer; #if RAZOR4 await Execute(); #else Execute(); #endif writer.Flush(); _context.CurrentWriter = null; if (Layout != null) { // Get the layout template. var layout = ResolveLayout(Layout); if (layout == null) { throw new ArgumentException("Template you are trying to run uses layout, but no layout found in cache or by resolver."); } // Push the current body instance onto the stack for later execution. var body = new TemplateWriter(tw => { StreamToTextWriter(memory, tw); }); context.PushBody(body); context.PushSections(); #if RAZOR4 await layout.Run(context, reader); #else layout.Run(context, reader); #endif return; } StreamToTextWriter(memory, reader); } } }
/// <summary> /// Runs the template and returns the result. /// </summary> /// <param name="context">The current execution context.</param> /// <param name="reader"></param> /// <returns>The merged result of the template.</returns> public async Task Run(ExecuteContext context, TextWriter reader) { _context = context; StringBuilder builder = new StringBuilder(); using (var writer = new StringWriter(builder)) { _context.CurrentWriter = writer; await ExecuteAsync(); writer.Flush(); _context.CurrentWriter = null; if (Layout != null) { // Get the layout template. var layout = ResolveLayout(Layout); if (layout == null) { throw new ArgumentException("Template you are trying to run uses layout, but no layout found in cache or by resolver."); } // Push the current body instance onto the stack for later execution. var body = new TemplateWriter(tw => tw.Write(builder.ToString())); context.PushBody(body); context.PushSections(); await layout.Run(context, reader); return; } reader.Write(builder.ToString()); } }