This repository was archived by the owner on Jul 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandlebarsRenderer.cs
More file actions
36 lines (33 loc) · 1.52 KB
/
HandlebarsRenderer.cs
File metadata and controls
36 lines (33 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace Red.HandlebarsRenderer
{
/// <summary>
/// Handlebars renderer extension using Handlebars.Net
/// </summary>
public static class HandlebarsRenderer
{
/// <summary>
/// Renders a Handlebars template file and sends it
/// </summary>
/// <param name="response">The instance of the response</param>
/// <param name="filePath">The path of the Handlebars template file to be rendered</param>
/// <param name="renderParams">The render parameter object which will be passed to the template renderer</param>
/// <param name="cacheRenderers">Whether to cache renderers. Recommended</param>
/// <param name="status">The status code for the response</param>
public static Task<HandlerType> RenderTemplate(this Response response, string filePath, object renderParams, bool cacheRenderers = true,
HttpStatusCode status = HttpStatusCode.OK)
{
response.AspNetResponse.StatusCode = (int) status;
response.AspNetResponse.ContentType = "text/html";
var renderer = HandlebarsCache.Instance.GetRenderer(filePath, cacheRenderers);
using (var writer = new StreamWriter(response.AspNetResponse.Body))
{
renderer(writer, renderParams);
}
return _cachedResult;
}
private static readonly Task<HandlerType> _cachedResult = Task.FromResult(HandlerType.Final);
}
}