This commit is contained in:
2020-08-24 21:51:57 +00:00
parent e763b79988
commit c5e7904dfe
2 changed files with 15 additions and 5 deletions

View File

@@ -104,7 +104,7 @@ namespace AyaNova.Api.Controllers
}";
//sample template
var reportTemplate = "'<div>{{#with person}}{{firstname}} {{lastname}}{{/with}}</div>'";
var reportTemplate = "'<div>{{#with person}}{{firstname}} {{loud lastname}}{{/with}}</div>'";
//data object
var reportData = "{ person: { firstname: 'Yehuda', lastname: 'Katz' } }";
@@ -114,13 +114,18 @@ namespace AyaNova.Api.Controllers
//Add handlebars JS for compiling and presenting
await page.AddScriptTagAsync(new AddTagOptions() { Path = hbspath });
//test add helpers
await page.AddScriptTagAsync(new AddTagOptions() { Path = Path.Combine(ReportJSFolderPath, "ay-hb-helpers.js") });
//execute to add to handlebars
await page.EvaluateExpressionAsync("ayRegisterHelpers();");
//compile and run handlebars template
var compileScript = $"Handlebars.compile({reportTemplate})({reportData});";
var compileScript = $"Handlebars.compile({reportTemplate})({reportData});";
var resultHTML = await page.EvaluateExpressionAsync<string>(compileScript);
//render report as HTML
await page.AddStyleTagAsync(new AddTagOptions { Content = reportCSS });
await page.SetContentAsync(resultHTML);
await page.AddStyleTagAsync(new AddTagOptions { Content = reportCSS });
await page.SetContentAsync(resultHTML);
//render to pdf and return
var pdfBuffer = await page.PdfDataAsync();

View File

@@ -0,0 +1,5 @@
function ayRegisterHelpers() {
Handlebars.registerHelper("loud", function (aString) {
return aString.toUpperCase();
});
}