-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (59 loc) · 1.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Module dependencies.
*/
var razor = require('razor');
var path = require('path');
/**
* Add a render() method to koa that allows
* you to render almost any templating engine.
*
* Example:
*
* app.use(views('./example', {
* html: 'underscore'
* }));
*
* // in your route handler
* this.body = yield this.render('index');
*
* @param {String} path
* @param {String} ext (optional)
* @param {Object} opts (optional)
* @return {Function} middleware
* @api public
*/
module.exports = function (dir) {
// middleware
return function *views(next) {
renderFn = render(dir);
this.render = renderFn;
yield next;
}
}
/**
* Determine `render()` function.
*
* @param {String} path
* @return {Generator} view
* @api private
*/
function render ( dir) {
return function (view, model){
var file = path.join(dir, view) + ".js.html";
return function(callback){
var ctx=this;
razor.findView(file, function(template){
if (template == undefined)
{
var err = new Error("Not Found");
err.status = 404;
err.expose = true;
callback(err, null);
} else
razor.render(template,model, null, function(html){
callback(null, html);
});
});
}
}
}