description and source-codeexpress-ejs-layouts = function (req, res, next) {
var render = res.render;
res.render = function (view, options, fn) {
var layout, self = this, app = req.app,
defaultLayout = app.get('layout');
options = options || {};
if (typeof options === 'function') {
fn = options;
options = {};
}
if (options.layout === false || ((options.layout || defaultLayout) === false)) {
render.call(res, view, options, fn);
return;
}
layout = options.layout || res.locals.layout || defaultLayout;
if (layout === true || layout === undefined) {
layout = 'layout';
}
options.contentFor = contentFor;
render.call(res, view, options, function (err, str) {
var l, locals;
if (err) { return fn ? fn(err) : next(err); }
locals = {
body: str,
defineContent: function(contentName) { return locals[contentName] || ''; }
};
for (l in options) {
if (options.hasOwnProperty(l) && l !== 'layout' && l !== 'contentFor') {
locals[l] = options[l];
}
}
if (options.extractScripts === true || (options.extractScripts === undefined && app.get('layout extractScripts') === true)) {
locals.script = '';
parseScripts(locals);
}
if (options.extractStyles === true || (options.extractStyles === undefined && app.get('layout extractStyles') === true)) {
locals.style = '';
parseStyles(locals);
}
if (options.extractMetas === true || (options.extractMetas === undefined && app.get('layout extractMetas') === true)) {
locals.meta = '';
parseMetas(locals);
}
parseContents(locals);
render.call(self, layout, locals, fn);
});
};
next();
}