| 37 | * @returns {Promise<HttpResponseOK>} |
| 38 | */ |
| 39 | export async function render(templatePath: string, locals: object = {}, dirname?: string): Promise<HttpResponseOK> { |
| 40 | const path = dirname ? join(dirname, templatePath) : templatePath; |
| 41 | const template = await readFile(path, 'utf8'); |
| 42 | |
| 43 | const templateEngine = Config.get('settings.templateEngine', 'string'); |
| 44 | if (templateEngine) { |
| 45 | const { __express } = require(templateEngine); |
| 46 | if (__express) { |
| 47 | return new Promise<HttpResponseOK>((resolve, reject) => { |
| 48 | __express(path, locals, (err: any, html: string) => { |
| 49 | if (err) { |
| 50 | return reject(err); |
| 51 | } |
| 52 | resolve(new HttpResponseOK(html)); |
| 53 | }); |
| 54 | }); |
| 55 | } |
| 56 | throw new Error(`${templateEngine} is not a template engine compatible with FoalTS.`); |
| 57 | } |
| 58 | |
| 59 | return new HttpResponseOK(renderToString(template, locals)); |
| 60 | } |