var cache = {};
/**
* Cache view contents to prevent I/O hits.
*
* @type Object
*/
var viewCache = {};
/**
* Clear the view cache.
*
* @api public
*/
exports.clearCache = function(){
viewCache = {};
};
/**
* Render `view` partial with the given `options`.
*
* Options:
* - `as` String name for the id used to which "collection" assign it's current value.
101 unmodified lines
Expand all
throw err;
}
}
// Render
fs.readFile(path, 'utf8', function(err, str){
if (err) {
error(err);
} else {
var engine = cache[ext] || (cache[ext] = require(ext.substr(1)));
try {
str = engine.render(str, options);
} catch (err) {
error(err);
}
if (layout) {
options.layout = false;
options.locals.body = str;
self.render(layout, options);
} else if (partial) {
return str;
} else if (fn) {
fn(null, str);
} else {
send(str);
}
}
});
// Cache contents
try {
var str = viewCache[path] || (viewCache[path] = fs.readFileSync(path, 'utf8'));
} catch (err) {
return error(err);
}
// Cache template engine exports
var engine = cache[ext] || (cache[ext] = require(ext.substr(1)));
// Attempt render
try {
str = engine.render(str, options);
} catch (err) {
return error(err);
}
// Layout support
if (layout) {
options.layout = false;
options.locals.body = str;
self.render(layout, options);
} else if (partial) {
return str;
} else if (fn) {
fn(null, str);
} else {
send(str);
}
};