-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
61 lines (48 loc) · 1.78 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
var hb = require('handlebars'),
fs = require('fs'),
path = require('path'),
templates = {};
// Sync walk in order to have all loaded before starting the service
function walk(dirname, fn) {
fs.readdirSync(dirname).forEach(function(fileName) {
fs.stat(dirname + '/' + fileName, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(dirname + '/' + fileName, fn);
} else {
fn(null, dirname + '/' + fileName);
}
});
});
}
module.exports = function(dirname) {
if (!dirname) throw new Error('handlebars() templates directory required');
var partials = path.resolve(path.join(dirname,'partials')),
helpers = path.resolve(path.join(dirname, 'helpers.js'));
// Loads all the .html from the partials directory
walk(partials, function(err, fileName) {
var ext = path.extname(fileName),
base = fileName.replace(partials + '/', '') // removes the partial dir
.replace(ext, '') // removes the extension
.replace('/', '.'); // Handlebars doesnt support partial names with slashes
if (ext === '.html') {
// register as partials
hb.registerPartial(base, fs.readFileSync(fileName, 'ascii'));
}
});
// register the helpers - it must be a nodejs module
fs.existsSync(helpers) && require(helpers)(hb);
// excecutes the template
var app = function(data) {
// Lazy load for templates
if (!templates[data.template]) {
templates[data.template] = hb.compile(
fs.readFileSync(path.join(dirname, data.template + '.html'), 'ascii'));
}
return templates[data.template](data.context);
}
// middleware for handling POST request
app.middleware = function(req, res, next) {
res.end(app(req.body));
}
return app;
};