-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.js
61 lines (52 loc) · 1.2 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*!
* assemble-handlebars <https://github.com/assemble/assemble-handlebars>
*
* Copyright (c) 2013-2017, Brian Woodward.
* Licensed under the MIT License.
*/
'use strict';
var helpers = require('handlebars-helpers');
var handlebars = require('handlebars');
// register built-in helpers
exports.init = function(options, params) {
options = options || {};
if (options.handlebars) {
handlebars = options.handlebars;
}
helpers(options);
};
exports.compile = function(str, context, cb) {
var fn;
try {
fn = handlebars.compile(str, context);
} catch (err) {
return cb(err);
}
cb(null, fn);
};
exports.render = function(template, context, cb) {
var res;
try {
if (typeof template === 'string') {
template = handlebars.compile(template, context);
}
res = template(context);
} catch (err) {
return cb(err);
}
cb(null, res);
};
exports.registerFunctions = function(fns) {
fns = fns || {};
for (var key in fns) {
if (fns.hasOwnProperty(key)) {
handlebars.registerHelper(key, fns[key]);
}
}
};
exports.registerPartial = function(name, str) {
try {
handlebars.registerPartial(name, str);
} catch (err) {}
};
exports.handlebars = handlebars;