Ejemplo n.º 1
0
        internal static async Task RenderAsync(RazorPage <dynamic> page)
        {
            var services         = page.Context.RequestServices;
            var liquidViewParser = services.GetRequiredService <LiquidViewParser>();
            var path             = Path.ChangeExtension(page.ViewContext.ExecutingFilePath, ViewExtension);
            var templateOptions  = services.GetRequiredService <IOptions <TemplateOptions> >().Value;

            var isDevelopment = services.GetRequiredService <IHostEnvironment>().IsDevelopment();
            var template      = await ParseAsync(liquidViewParser, path, templateOptions.FileProvider, Cache, isDevelopment);

            var context     = new LiquidTemplateContext(services, templateOptions);
            var htmlEncoder = services.GetRequiredService <HtmlEncoder>();

            try
            {
                // Defer the buffer disposing so that a template can be rendered twice.
                var content = new ViewBufferTextWriterContent(releaseOnWrite: false);
                ShellScope.Current.RegisterBeforeDispose(scope => content.Dispose());

                await context.EnterScopeAsync(page.ViewContext, (object)page.Model);

                await template.FluidTemplate.RenderAsync(content, htmlEncoder, context);

                // Use ViewBufferTextWriter.Write(object) from ASP.NET directly since it will use a special code path
                // for IHtmlContent. This prevent the TextWriter methods from copying the content from our buffer
                // if we did content.WriteTo(page.Output)

                page.Output.Write(content);
            }
            finally
            {
                context.ReleaseScope();
            }
        }
        public static async Task RenderAsync(this LiquidViewTemplate template, TextWriter writer, TextEncoder encoder, LiquidTemplateContext context, object model, Action <Scope> scopeAction)
        {
            try
            {
                await context.EnterScopeAsync(model, scopeAction);

                await template.RenderAsync(writer, encoder, context);
            }
            finally
            {
                context.ReleaseScope();
            }
        }
        public static async Task <string> RenderAsync(this LiquidViewTemplate template, TextEncoder encoder, LiquidTemplateContext context, object model, Action <Scope> scopeAction)
        {
            try
            {
                await context.EnterScopeAsync(model, scopeAction);

                return(await template.RenderAsync(context, encoder));
            }
            finally
            {
                context.ReleaseScope();
            }
        }