Skip to content

Commit

Permalink
seeding repo with express extension that adds layout cability for ejs
Browse files Browse the repository at this point in the history
  • Loading branch information
mcwhittemore committed May 23, 2013
0 parents commit c82146a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
67 changes: 67 additions & 0 deletions index.js
@@ -0,0 +1,67 @@
var layout = function(res, data, blocks, callback){

var blockNames = Object.keys(blocks);
if(blockNames.length==0){
callback(data);
}
else{
var blockName = blockNames[0];
var block = blocks[blockName];
delete blocks[blockName];

if(block.block==undefined || typeof block.block != "string"){
throw "Block "+blockName+" must have a block attribute of type string";
}

var rd = block.data;
if(rd==undefined){
rd = {};
}

res.render(block.block, rd, function(err, html){
if(err){
throw err;
}

data[blockName] = html;
layout(res, data, blocks, callback);
});

}
}

module.exports.express = function(req, res, next){


res.layout = function(layout_file, data, blocks, callback){
console.log(Object.keys(this));
if(data==undefined){
res.render(layout_file);
return;
}

if(blocks==undefined){
res.render(layout_file, data);
return;
}
else if(typeof blocks == "function"){
res.render(layout_file, data, blocks);
return;
}
else if(typeof blocks == "object"){
layout(res, data, blocks, function(new_data){
if(callback==undefined){
res.render(layout_file, new_data);
}
else{
res.render(layout_file, new_data, callback);
}
});
}
else {
throw "Invalid Block Type";
}
}

next();
}
24 changes: 24 additions & 0 deletions package.json
@@ -0,0 +1,24 @@
{
"name": "ejs-layouts",
"version": "0.0.1",
"author": "Matthew Chase Whittemore <mcwhittemore@gmail.com>",
"description": "add layout options for ejs to frameworks",
"main": "index",
"contributors": [
{
"name": "Matthew Chase Whittemore",
"email": "mcwhittemore@gmail.com"
}
],
"repository": {
"type": "git",
"url": ""
},
"keywords": [
"ejs",
"layout",
"layouts",
"express",
"blocks"
]
}

0 comments on commit c82146a

Please sign in to comment.