-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
38 lines (31 loc) · 1005 Bytes
/
plugin.js
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
37
38
"use strict";
const consolidate = require('consolidate');
const resolve = require('path').resolve;
exports.register = (server, options, next) => {
var internals = {};
server.decorate('server', 'consolidate', options => {
internals = options;
});
/**
* consolidate template rendering for Hapi.js
* @param {string} filename - Name of template file to be rendered.
* @param {object} context - The context for the template.
*/
const render = function(template, context) {
context = Object.assign( context || {}, internals.options, {template: template} );
template = `${resolve(internals.path, template)}.${internals.extension}`;
consolidate[internals.name](template, context)
.then(html => {
return this.response(html);
}).catch(err => {
console.error(err);
}).bind(this);
};
server.decorate('reply', 'render', render);
next();
};
exports.register.attributes = {
connections: false,
once: true,
pkg: require('./package.json')
};