Skip to content

Commit c22d353

Browse files
committed
Merge branch 'flush'
2 parents fe6125b + 692d6de commit c22d353

File tree

12 files changed

+450
-134
lines changed

12 files changed

+450
-134
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,25 @@ The `RazorTemplate` base class provides `Render` and `RenderAsync` methods to ex
196196

197197
Templates are stateful and not thread-safe, so it is advised to always create new instances of the templates to render.
198198

199+
### Flushing partial output
200+
201+
By default, the output of a template is buffered while it is executing, then copied to the provided writer when finished. This is necessary for features such as layouts to be supported, but may not always be desired.
202+
203+
The `RazorTemplate` class provides a `FlushAsync` method which will copy the buffered output to the provided `TextWriter` and then flush the writer:
204+
205+
<!-- snippet: TemplateWithFlush.Usage -->
206+
<a id='snippet-templatewithflush.usage'></a>
207+
```cshtml
208+
<div>Lightweight content goes here.</div>
209+
@await FlushAsync()
210+
<div>Slower to render content goes here.</div>
211+
```
212+
<sup><a href='/src/RazorBlade.IntegrationTest/Examples/TemplateWithFlush.cshtml#L2-L6' title='Snippet source file'>snippet source</a> | <a href='#snippet-templatewithflush.usage' title='Start of snippet'>anchor</a></sup>
213+
<!-- endSnippet -->
214+
215+
> [!IMPORTANT]
216+
> Flushing is not compatible with layout usage.
217+
199218
### MSBuild
200219

201220
The source generator will process `RazorBlade` MSBuild items which have the `.cshtml` file extension.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@inherits RazorBlade.HtmlTemplate
2+
@* begin-snippet: TemplateWithFlush.Usage *@
3+
<div>Lightweight content goes here.</div>
4+
@await FlushAsync()
5+
<div>Slower to render content goes here.</div>
6+
@* end-snippet *@
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@inherits RazorBlade.HtmlTemplate
2+
<h2>Hello, world!</h2>
3+
This is the body contents before flushing.
4+
@await FlushAsync()
5+
This is the body contents after flushing.
6+
@await FlushAsync()
7+
This is the body contents after a second flushing.

src/RazorBlade.IntegrationTest/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public static void Main()
1010
WriteTemplate(new TestTemplate { Name = "World" });
1111
WriteTemplate(new TestTemplateWithModel(new FooBarModelClass { Foo = "Foo", Bar = "Bar" }));
1212
WriteTemplate(new PageWithLayout());
13+
WriteTemplate(new PageWithFlush());
1314
}
1415

1516
private static void WriteTemplate(RazorTemplate template)

src/RazorBlade.Library/HtmlLayout.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ async Task<IRazorExecutionResult> IRazorLayout.ExecuteLayoutAsync(IRazorExecutio
2121
try
2222
{
2323
_layoutInput = input;
24-
return await ExecuteAsyncCore(input.CancellationToken);
24+
return await ExecuteAsyncCore(null, input.CancellationToken);
2525
}
2626
finally
2727
{
2828
_layoutInput = null;
2929
}
3030
}
3131

32-
private protected override Task<IRazorExecutionResult> ExecuteAsyncCore(CancellationToken cancellationToken)
32+
private protected override Task<IRazorExecutionResult> ExecuteAsyncCore(TextWriter? targetOutput, CancellationToken cancellationToken)
3333
{
3434
if (_layoutInput is null)
3535
throw new InvalidOperationException(_contentsRequiredErrorMessage);
3636

37-
return base.ExecuteAsyncCore(cancellationToken);
37+
return base.ExecuteAsyncCore(targetOutput, cancellationToken);
3838
}
3939

4040
/// <summary>

0 commit comments

Comments
 (0)