-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
seeding repo with express extension that adds layout cability for ejs
- Loading branch information
0 parents
commit c82146a
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
] | ||
} |