diff --git a/examples/templates.js b/examples/templates.js new file mode 100644 index 0000000..05bac98 --- /dev/null +++ b/examples/templates.js @@ -0,0 +1,43 @@ + +var views = require('co-views'); +var http = require('http'); +var koa = require('..'); +var app = koa(); + +// setup views + +var render = views('examples/templates', { + ext: 'ejs' +}); + +// dummy data + +var user = { + name: { + first: 'Tobi', + last: 'Holowaychuk' + }, + species: 'ferret', + age: 3 +}; + +// logger + +app.use(function(next){ + return function *logger(){ + var start = new Date; + yield next; + var ms = new Date - start; + console.log('%s %s - %s', this.method, this.url, ms); + } +}); + +// render + +app.use(function(next){ + return function *(){ + this.body = yield render('user', { user: user }); + } +}) + +app.listen(4000); diff --git a/examples/templates/user.ejs b/examples/templates/user.ejs new file mode 100644 index 0000000..cb64c1e --- /dev/null +++ b/examples/templates/user.ejs @@ -0,0 +1 @@ +
<%= user.name.first %> is a <%= user.age %> year old <%= user.species %>.
\ No newline at end of file diff --git a/package.json b/package.json index 220810d..24f5d1f 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,6 @@ "middleware", "rack" ], - "repository": "koajs/koa", "license": "MIT", "dependencies": { "co": "1.5.1", @@ -35,7 +34,9 @@ "should": "1.2.2", "mocha": "1.12.0", "supertest": "0.7.1", - "co-fs": "~1.0.1" + "co-fs": "~1.0.1", + "co-views": "0.0.1", + "ejs": "~0.8.4" }, "engines": { "node": "> 0.11.4"