forked from argo/argo-formatter-handlebars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlebars.js
34 lines (27 loc) · 795 Bytes
/
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
var fs = require('fs');
var Handlebars = require('handlebars');
var HandlebarsEngine = function() {
this.name = 'handlebars';
this.mediaTypes = ['text/html'];
this.extension = '.handlebars';
this.subdirectory = '/html';
this.cache = {};
};
HandlebarsEngine.prototype.format = function(filename, locals, cb) {
if (this.cache.hasOwnProperty(filename)) {
var template = this.cache[filename];
var body = template(locals);
return cb(null, body);
}
var self = this;
fs.readFile(filename, function(err, contents) {
if (err) {
return cb(err);
}
var template = Handlebars.compile(contents.toString());
self.cache[filename] = template;
var body = template(locals);
return cb(null, body);
});
};
module.exports = new HandlebarsEngine();