forked from assemble/assemble-handlebars
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
80 lines (67 loc) · 1.66 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*!
* assemble-handlebars
* https://github.com/assemble/assemble-handlebars
*
* Copyright (c) 2013 Assemble
* Licensed under the MIT license.
*/
var _ = require('lodash');
var handlebars = require('handlebars');
var helpers = null;
try {
helpers = require('handlebars-helpers');
} catch (ex) {
console.log('WARNING: ', ex);
console.log('To use handlebars-helpers run `npm install handlebars-helpers`');
}
var plugin = function() {
'use strict';
var init = function(options, params) {
// register built-in helpers
if(helpers && helpers.register) {
helpers.register(handlebars, options, params);
}
};
var compile = function(src, options, callback) {
var tmpl;
try {
tmpl = handlebars.compile(src, options);
} catch(ex) {
callback(ex, null);
}
callback(null, tmpl);
};
var render = function(tmpl, options, callback) {
var content;
try {
if(typeof tmpl === 'string') {
tmpl = handlebars.compile(tmpl, options);
}
content = tmpl(options);
} catch (ex) {
callback(ex, null);
}
callback(null, content);
};
var registerFunctions = function(helperFunctions) {
if(helperFunctions) {
_.forOwn(helperFunctions, function(fn, key) {
handlebars.registerHelper(key, fn);
});
}
};
var registerPartial = function(filename, content) {
try {
handlebars.registerPartial(filename, content);
} catch (ex) {}
};
return {
init: init,
compile: compile,
render: render,
registerFunctions: registerFunctions,
registerPartial: registerPartial,
handlebars: handlebars
};
};
module.exports = exports = plugin();