124 lines
3.5 KiB
JavaScript
124 lines
3.5 KiB
JavaScript
/**
|
|
* Support utilities for rendering html pages and emails
|
|
* It is based on [pug](https://pugjs.org) - previously jade - and retains
|
|
* a similar api to jade.renderFile() but leverages pre-compiled functions for
|
|
* performance.
|
|
*/
|
|
'use strict';
|
|
|
|
const _ = require('lodash');
|
|
const pug = require('pug');
|
|
const Handlebars = require('handlebars/runtime');
|
|
const path = require('path');
|
|
|
|
module.exports = {
|
|
initTemplates: initTemplates,
|
|
render: render
|
|
};
|
|
|
|
/**
|
|
* Location for the root templates directory
|
|
*/
|
|
const PUG_TEMPLATE_DIR = path.join(global.pathPrefix, '..', 'pug');
|
|
const HANDLEBARS_TEMPLATE_DIR = path.join(global.pathPrefix, '..', 'email_templates');
|
|
|
|
/**
|
|
* List of templates that we have available for use
|
|
*/
|
|
const PUG_TEMPLATES = [
|
|
/* HTML templates for Register7. Remove as part of T1469 */
|
|
'templates/10005_reg_deleted.pug',
|
|
'templates/54_email_not_found.pug',
|
|
'templates/56_mobile_number_not_found.pug',
|
|
'templates/57_association_error.pug',
|
|
'templates/58_fully_registered.pug',
|
|
'templates/undef_database_offline.pug',
|
|
|
|
/* Admin notifications */
|
|
'adminNotifier/identity_check.pug',
|
|
'adminNotifier/credits_low.pug'
|
|
];
|
|
|
|
const HANDLEBARS_TEMPLATES = [
|
|
/* Emails */
|
|
'account-locked',
|
|
'account-recovery',
|
|
'bridge-welcome',
|
|
'device-added',
|
|
'device-locked',
|
|
'device-new-hardware',
|
|
'device-re-registration',
|
|
'email-changed-old',
|
|
'email-changed-new',
|
|
'email-reverted-from',
|
|
'email-reverted-to',
|
|
'invoice-new',
|
|
'invoice-cancelled',
|
|
'invoice-queried',
|
|
'invoice-updated',
|
|
'marketing-generic',
|
|
'password-changed',
|
|
'password-changed-web',
|
|
'pin-reset',
|
|
'thoughtful-enterprises-marketing'
|
|
];
|
|
|
|
/**
|
|
* Object to store the rendered template functions for later use
|
|
*/
|
|
var renderedTemplates = {};
|
|
|
|
/**
|
|
* Pre-loads and compiles the pug templates into render functions.
|
|
*/
|
|
function initTemplates() {
|
|
//
|
|
// Init the pug templates
|
|
//
|
|
for (let i = 0; i < PUG_TEMPLATES.length; ++i) {
|
|
/* Find the full path to the template */
|
|
let templatePath = path.join(PUG_TEMPLATE_DIR, PUG_TEMPLATES[i]);
|
|
|
|
/* compile the render function */
|
|
let renderFunc = pug.compileFile(templatePath);
|
|
|
|
/* store it in the cache */
|
|
renderedTemplates[PUG_TEMPLATES[i]] = renderFunc;
|
|
}
|
|
|
|
//
|
|
// Init the handlebars templates
|
|
//
|
|
for (let i = 0; i < HANDLEBARS_TEMPLATES.length; ++i) {
|
|
/* Find the full path to the template */
|
|
let templatePath = path.join(HANDLEBARS_TEMPLATE_DIR, HANDLEBARS_TEMPLATES[i]);
|
|
|
|
/**
|
|
* Require the template. This returns the template render function
|
|
*/
|
|
let renderFunc = require(templatePath);
|
|
|
|
/* store it in the cache */
|
|
renderedTemplates[HANDLEBARS_TEMPLATES[i]] = renderFunc;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Renders the given template using the provided data. Uses the cached render
|
|
* functions that are generated by initTemplates()
|
|
*
|
|
* @param {String} templateName - the name of the template, e.g. `account-locked`
|
|
* @param {Object} data - the data to render into the template
|
|
*
|
|
* @throws {Error} - throws an Error if the template hasn't been loaded
|
|
* @returns {String} - the rendered HTML
|
|
*/
|
|
function render(templateName, data) {
|
|
var renderFunc = renderedTemplates[templateName];
|
|
if (!_.isFunction(renderFunc)) {
|
|
throw new Error('Template [' + templateName + '] doesn\'t exist.');
|
|
}
|
|
|
|
return renderFunc(data);
|
|
}
|