-
Notifications
You must be signed in to change notification settings - Fork 167
/
handlebars.js
40 lines (32 loc) · 1.06 KB
/
handlebars.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
39
40
'use strict';
const Handlebars = require('handlebars');
const _ = require('lodash');
module.exports = function(source, config){
config = config || {};
let viewsLoaded = false;
_.each(config.helpers || {}, function(helper, name){
Handlebars.registerHelper(name, helper);
});
_.each(config.partials || {}, function(partial, name){
Handlebars.registerPartial(name, partial);
});
function loadViews(source) {
for (let item of source.flatten(true)) {
Handlebars.registerPartial(item.handle, item.content);
if (item.alias) {
Handlebars.registerPartial(item.alias, item.content);
}
}
viewsLoaded = true;
}
source.on('loaded', loadViews);
source.on('changed', loadViews);
return {
engine: Handlebars,
render: function(path, str, context, meta){
if (!viewsLoaded) loadViews(source);
var template = Handlebars.compile(str);
return Promise.resolve(template(context));
}
}
};