-
Notifications
You must be signed in to change notification settings - Fork 16
/
__express.js
42 lines (39 loc) · 1.22 KB
/
__express.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
var fs = require('fs');
var resolve = require('path').resolve;
var whiskers = require('./whiskers');
module.exports = function(path, options, fn) {
var templates = [path];
var partials = ['']; // empty string at index 0
for (var p in options.partials) {
if (typeof options.partials[p] !== "function") {
partials.push(p);
templates.push(resolve(options.settings.views, options.partials[p]));
}
}
var pending = templates.length;
function compile(path, fn) {
var compiled = whiskers.cache[path];
// cached
if (options.cache && compiled) return fn(null, compiled);
// read
fs.readFile(path, 'utf8', function(err, str){
if (err) return fn(err);
compiled = whiskers.compile(str);
if (options.cache) whiskers.cache[path] = compiled;
fn(null, compiled);
});
}
templates.forEach(function(template, i){
compile(template, function(err, compiled){
if (err) return fn(err);
// the view is the first template
if (i == 0) {
path = compiled;
} else {
options.partials[partials[i]] = compiled;
if (!options[partials[i]]) options[partials[i]] = compiled;
}
--pending || fn(null, path(options));
});
});
};