Permalink
Browse files

exports.middleware() 增强的res.render()函数

  • Loading branch information...
1 parent f02785b commit a7acf39ab18d89fc766185c14723e0b8c81d387c @leizongmin committed Nov 3, 2014
Showing with 20 additions and 2 deletions.
  1. +19 −1 lib/index.js
  2. +1 −1 package.json
View
@@ -285,4 +285,22 @@ module.exports = exports = function (options) {
exports.tinyliquid = tinyliquid;
// Create a new context
-exports.newContext = tinyliquid.newContext;
+exports.newContext = tinyliquid.newContext;
+
+// render function middleware
+exports.middleware = function (req, res, next) {
+ var tempRenderName = '_tinyliquid_original_render_' + Math.random();
+ res[tempRenderName] = res.render;
+ res.context = tinyliquid.newContext();
+ res.render = function (view, data, callback) {
+ data = data || {};
+ Object.keys(res.locals).forEach(function (k) {
+ res.context.setLocals(k, res.locals[k]);
+ });
+ Object.keys(data).forEach(function (k) {
+ res.context.setLocals(k, data[k]);
+ });
+ res[tempRenderName](view, {context: res.context}, callback);
+ };
+ next();
+};
View
@@ -1,7 +1,7 @@
{
"name": "express-liquid",
"main": "./lib/index.js",
- "version": "0.2.5",
+ "version": "0.2.6",
"description": "A liquid template engine for Express",
"keywords": ["liquid", "template"],
"author": "Lei Zongmin <leizongmin@gmail.com>",

0 comments on commit a7acf39

Please sign in to comment.