add template integration example
This commit is contained in:
parent
60576832df
commit
6a51f73524
3 changed files with 47 additions and 2 deletions
43
examples/templates.js
Normal file
43
examples/templates.js
Normal file
|
@ -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);
|
1
examples/templates/user.ejs
Normal file
1
examples/templates/user.ejs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<p><%= user.name.first %> is a <%= user.age %> year old <%= user.species %>.</p>
|
|
@ -19,7 +19,6 @@
|
||||||
"middleware",
|
"middleware",
|
||||||
"rack"
|
"rack"
|
||||||
],
|
],
|
||||||
"repository": "koajs/koa",
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"co": "1.5.1",
|
"co": "1.5.1",
|
||||||
|
@ -35,7 +34,9 @@
|
||||||
"should": "1.2.2",
|
"should": "1.2.2",
|
||||||
"mocha": "1.12.0",
|
"mocha": "1.12.0",
|
||||||
"supertest": "0.7.1",
|
"supertest": "0.7.1",
|
||||||
"co-fs": "~1.0.1"
|
"co-fs": "~1.0.1",
|
||||||
|
"co-views": "0.0.1",
|
||||||
|
"ejs": "~0.8.4"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "> 0.11.4"
|
"node": "> 0.11.4"
|
||||||
|
|
Loading…
Reference in a new issue