Created
October 1, 2012 11:59
-
-
Save deoxxa/3811204 to your computer and use it in GitHub Desktop.
ginger middleware thing
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
// app setup | |
app.configure(function() { | |
app.use(require("./send_response")(path.join(__dirname, "views"))); | |
}); | |
// wheee | |
app.get("/", function(req, res, next) { | |
return res.send_response("some/template.html", {a: "b", c: "d"}); | |
}); |
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
var fs = require("fs"), | |
path = require("path"), | |
Ginger = require("ginger"); | |
module.exports = function(root) { | |
var compiler = new Ginger.Compiler(), | |
ctx = new Ginger.Context(); | |
ctx.on_not_found = function(name, cb) { | |
console.log("parsing template " + name); | |
fs.readFile(path.join(root, name + ".ginger"), function(err, data) { | |
if (err) { | |
return cb(err); | |
} | |
var parsed = Ginger.Parser.parse(data.toString()); | |
var compiled = compiler.compile(parsed); | |
var fn = new Function("ctx", "cb", compiled); | |
cb(null, fn); | |
}); | |
}; | |
var send_response = function(template, data) { | |
data = data || {}; | |
if (this.req.xhr) { | |
return this.json({template: template, data: data}); | |
} | |
var self = this; | |
ctx.create_child(data).render(template, function(err, data) { | |
self.send(data); | |
}); | |
}; | |
return function(req, res, next) { | |
res.send_response = send_response; | |
return next(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment