From 1f765b3bdacd600408c73e4fa63d90c7dc82bfd2 Mon Sep 17 00:00:00 2001 From: matthewwren Date: Mon, 13 Jan 2020 13:50:37 +0000 Subject: [PATCH] My suggestion for fixing issue 287 --- src/RazorLight/EngineHandler.cs | 8 ++-- src/RazorLight/TemplateRenderer.cs | 48 ++++++++----------- .../RazorLight.Tests/TemplateRendererTest.cs | 8 ++-- 3 files changed, 28 insertions(+), 36 deletions(-) diff --git a/src/RazorLight/EngineHandler.cs b/src/RazorLight/EngineHandler.cs index 3815f5f6..d4af1dfb 100644 --- a/src/RazorLight/EngineHandler.cs +++ b/src/RazorLight/EngineHandler.cs @@ -100,8 +100,8 @@ public async Task RenderTemplateAsync( using (var scope = new MemoryPoolViewBufferScope()) { - var renderer = new TemplateRenderer(templatePage, this, HtmlEncoder.Default, scope); - await renderer.RenderAsync().ConfigureAwait(false); + var renderer = new TemplateRenderer(this, HtmlEncoder.Default, scope); + await renderer.RenderAsync(templatePage).ConfigureAwait(false); } } @@ -113,9 +113,7 @@ public async Task RenderIncludedTemplateAsync( TemplateRenderer templateRenderer) { SetModelContext(templatePage, textWriter, model, viewBag); - - templateRenderer.RazorPage = templatePage; - await templateRenderer.RenderAsync().ConfigureAwait(false); + await templateRenderer.RenderAsync(templatePage).ConfigureAwait(false); } /// diff --git a/src/RazorLight/TemplateRenderer.cs b/src/RazorLight/TemplateRenderer.cs index 50f236d1..f597d2c2 100644 --- a/src/RazorLight/TemplateRenderer.cs +++ b/src/RazorLight/TemplateRenderer.cs @@ -14,36 +14,29 @@ public class TemplateRenderer private readonly IEngineHandler _engineHandler; private readonly IViewBufferScope _bufferScope; - public TemplateRenderer( - ITemplatePage razorPage, + public TemplateRenderer( IEngineHandler engineHandler, HtmlEncoder htmlEncoder, IViewBufferScope bufferScope) - { - RazorPage = razorPage ?? throw new ArgumentNullException(nameof(razorPage)); + { _engineHandler = engineHandler ?? throw new ArgumentNullException(nameof(engineHandler)); _bufferScope = bufferScope ?? throw new ArgumentNullException(nameof(bufferScope)); _htmlEncoder = htmlEncoder ?? throw new ArgumentNullException(nameof(htmlEncoder)); } - /// - /// Gets instance that the views executes on. - /// - public ITemplatePage RazorPage { get; set; } + ///// + ///// Gets the sequence of _ViewStart instances that are executed by this view. + ///// + //public IReadOnlyList ViewStartPages { get; } - ///// - ///// Gets the sequence of _ViewStart instances that are executed by this view. - ///// - //public IReadOnlyList ViewStartPages { get; } + /// + public virtual async Task RenderAsync(ITemplatePage page) + { + var context = page.PageContext; - /// - public virtual async Task RenderAsync() - { - var context = RazorPage.PageContext; - - var bodyWriter = await RenderPageAsync(RazorPage, context, invokeViewStarts: false).ConfigureAwait(false); - await RenderLayoutAsync(context, bodyWriter).ConfigureAwait(false); - } + var bodyWriter = await RenderPageAsync(page, context, invokeViewStarts: false).ConfigureAwait(false); + await RenderLayoutAsync(page, context, bodyWriter).ConfigureAwait(false); + } private async Task RenderPageAsync( ITemplatePage page, @@ -151,14 +144,15 @@ private Task RenderViewStartsAsync(PageContext context) //} } - private async Task RenderLayoutAsync( + private async Task RenderLayoutAsync( + ITemplatePage page, PageContext context, - ViewBufferTextWriter bodyWriter) - { - // A layout page can specify another layout page. We'll need to continue - // looking for layout pages until they're no longer specified. - var previousPage = RazorPage; - var renderedLayouts = new List(); + ViewBufferTextWriter bodyWriter) + { + // A layout page can specify another layout page. We'll need to continue + // looking for layout pages until they're no longer specified. + var previousPage = page; + var renderedLayouts = new List(); // This loop will execute Layout pages from the inside to the outside. With each // iteration, bodyWriter is replaced with the aggregate of all the "body" content diff --git a/tests/RazorLight.Tests/TemplateRendererTest.cs b/tests/RazorLight.Tests/TemplateRendererTest.cs index cbe0ebf8..28d789eb 100644 --- a/tests/RazorLight.Tests/TemplateRendererTest.cs +++ b/tests/RazorLight.Tests/TemplateRendererTest.cs @@ -31,8 +31,8 @@ public async Task Ensure_PrerenderCallbacks_Are_Invoked() engineMock.SetupGet(e => e.Options).Returns(options); //Act - var templateRenderer = new TemplateRenderer(page, engineMock.Object, HtmlEncoder.Default, new MemoryPoolViewBufferScope()); - await templateRenderer.RenderAsync(); + var templateRenderer = new TemplateRenderer(engineMock.Object, HtmlEncoder.Default, new MemoryPoolViewBufferScope()); + await templateRenderer.RenderAsync(page); //Assert Assert.True(triggered1); @@ -84,8 +84,8 @@ public async Task Template_Shares_Context_With_Layout() using (var writer = new StringWriter()) { page.PageContext.Writer = writer; - var renderer = new TemplateRenderer(page, engineMock.Object, encoder, new MemoryPoolViewBufferScope()); - await renderer.RenderAsync(); + var renderer = new TemplateRenderer(engineMock.Object, encoder, new MemoryPoolViewBufferScope()); + await renderer.RenderAsync(page); output = writer.ToString(); }