Register custom helper for docx template
-
Hi,
I would like to use some custom helper in the docx but I cant find the documentation on that. I try to register that in beforeRender script but seems within the docx file it doesnt recognize that.
-
OK I sorted it out. As it is run in worker with another context, need to parse the loading code as string in the req.template.helpers during beforeRender function for the engine to load it inside the context.
async function beforeRender (req, res) { req.template.helpers = ` const handlebars = require('handlebars'); const HandlebarsIntl = require('handlebars-intl') HandlebarsIntl.registerWith(handlebars) const helpers = require('handlebars-helpers')({ handlebars: handlebars }); `; }
-
Custom helpers can be registered directly in the template through the studio
https://jsreport.net/learn/docx#custom-helpers
There you can register handlebars helper libs
https://jsreport.net/learn/handlebars#thirdparty-helper-libraries