-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
45 lines (35 loc) · 1 KB
/
index.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
41
42
43
44
45
var through = require('through2');
var _ = require('underscore');
var hbs = require('handlebars');
module.exports = function(data, config) {
var options = _.extend({
layout: null,
partials: [],
helpers: []
}, config);
data = data || {};
var build = function(file, encoding, callback) {
var fileContents = file.contents.toString();
var template = '';
if (options.helpers.length > 0) {
_.each(options.helpers, function(helper) {
hbs.registerHelper(helper.name, helper.fn);
});
}
if (options.partials.length > 0) {
_.each(options.partials, function(partial) {
hbs.registerPartial(partial.name, partial.tpl);
});
}
if (_.isString(options.layout) && options.layout.indexOf('{{> body') !== -1) {
hbs.registerPartial('body', fileContents);
template = hbs.compile(options.layout);
} else {
template = hbs.compile(fileContents);
}
file.contents = new Buffer(template(data));
return callback(null, file);
};
return through.obj(build);
};
module.exports.hbs = hbs;