-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (46 loc) · 1.83 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
var hogan = require('hogan.js');
var fs = require('fs');
var path = require('path');
var cache = {};
var options = {
cache: true
};
function getTemplate(filePath, cacheEnabled, hoganOptions) {
if(!cacheEnabled || cache[filePath] === undefined) {
var content = '';
try {
content = fs.readFileSync(filePath, 'utf8');
} catch(e) {}
if(!cacheEnabled) return hogan.compile(content, hoganOptions);
cache[filePath] = hogan.compile(content, hoganOptions);
}
return cache[filePath];
}
function getPartials(partialPaths, basedir, ext, cacheEnabled, hoganOptions, partials) {
if(!partials) partials = {};
if(ext.length > 0 && ext.substring(0, 1) != '.') ext = '.' + ext;
for(var partialPath in partialPaths) {
var p = path.join(basedir, partialPaths[partialPath] + ext);
partials[partialPath] = getTemplate(p, cacheEnabled, hoganOptions);
}
return partials;
}
module.exports.setCacheEnabled = function(b) {
options.cache = b;
};
module.exports.clearCache = function() {
cache = {};
};
module.exports.__express = function(filePath, options, callback) {
var basedir = options.settings['views'];
var ext = options.settings['view engine'] || 'html';
var cacheEnabled = options.settings['hogan cache'] !== undefined ? options.settings['hogan cache'] : options.cache;
var hoganOptions = options.settings['hogan options'] || {};
var partialPaths = options.settings['partials'] || {};
var partials = getPartials(partialPaths, basedir, ext, cacheEnabled, hoganOptions);
if(options.partials !== undefined) {
partials = getPartials(options.partials, basedir, ext, cacheEnabled, hoganOptions, partials);
}
var template = getTemplate(filePath, cacheEnabled, hoganOptions);
callback(null, template.render(options, partials));
};