I have a simple page with head, menu, content and footer. I need to divide them into separate files. After reading through express documentation i (created 4 templates and) wrote something like this:
app.get('/', function(req, res) {
var response = [null, null, null, null]
, everyNotNull = function(elem) {
return (elem !== null);
}, sendResponse = function(type, str) {
switch (type) {
case 'head' : response[0] = str; break;
case 'menu' : response[1] = str; break;
case 'content' : response[2] = str; break;
case 'footer' : response[3] = str; break;
}
if (response.every(everyNotNull)) {
res.send(response.join(''));
}
};
res.partial('head', {'title' : 'page title'}, function(err, str) {
sendResponse('head', str);
});
res.partial('menu', {'active' : '/'}, function(err, str) {
sendResponse('menu', str);
});
res.partial('index', {'title' : 'Home'}, function(err, str) {
sendResponse('content', str);
});
res.partial('footer', {'nowdate' : (new Date()).getFullYear()}, function(err, str) {
sendResponse('footer', str);
});
});
Though it works it seems a bit dirty to me. Is there a better way to use partial templates?