-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplateLoader.js
49 lines (44 loc) · 1.39 KB
/
templateLoader.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
var Handlebars = require("handlebars")
var fs = require("fs")
// set up templates
var templates = {}
function loadTemplates(callback) {
fs.readdir(__dirname + "/views", (err, files) => {
var fileCount = 0
files.forEach((file, i) => {
var templateName = file.split(".")[0]
fs.readFile(__dirname + "/views/"+file, "utf8", (err, data) => {
templates[templateName] = Handlebars.compile(data)
Handlebars.unregisterPartial(templateName)
Handlebars.registerPartial(templateName, data)
fileCount++
if (fileCount == files.length && callback) {
callback()
}
})
})
})
}
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
switch (operator) {
case '==':
return (v1 == v2) ? options.fn(this) : options.inverse(this);
case '===':
return (v1 === v2) ? options.fn(this) : options.inverse(this);
case '<':
return (v1 < v2) ? options.fn(this) : options.inverse(this);
case '<=':
return (v1 <= v2) ? options.fn(this) : options.inverse(this);
case '>':
return (v1 > v2) ? options.fn(this) : options.inverse(this);
case '>=':
return (v1 >= v2) ? options.fn(this) : options.inverse(this);
case '&&':
return (v1 && v2) ? options.fn(this) : options.inverse(this);
case '||':
return (v1 || v2) ? options.fn(this) : options.inverse(this);
default:
return options.inverse(this);
}
});
module.exports = {loadTemplates,templates}