Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nswbmw/co-ejs
Koa ejs view render middleware.
https://github.com/nswbmw/co-ejs
Last synced: 2 months ago
JSON representation
Koa ejs view render middleware.
- Host: GitHub
- URL: https://github.com/nswbmw/co-ejs
- Owner: nswbmw
- Created: 2015-01-23T15:59:35.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-07-15T11:21:54.000Z (over 6 years ago)
- Last Synced: 2024-09-29T05:22:46.251Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 22.5 KB
- Stars: 8
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
- awesome-koa - co-ejs - koa ejs视图渲染中间件。 ![](https://img.shields.io/github/stars/nswbmw/co-ejs.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/co-ejs.svg?style=flat-square) (仓库 / 中间件)
README
## co-ejs
Koa ejs view render middleware.
### Install
npm i co-ejs --save
### Example
```
var path = require('path');var koa = require('koa');
var wait = require('co-wait');
var render = require('..');var app = koa();
var locals = {
version: '1.0.0',
now: function () {
return new Date();
},
ip: function *() {
yield wait(1000);
return this.ip || '127.0.0.1
';
},
callback: function() {
return function (cb) {
cb(null, 'callback
');
}
},
callbackGen: function() {
return function* () {
yield wait(3000);
return 'callbackGen
';
};
},
doNothing: function() {
console.log('this will not print');
}
};var filters = {
format: function (time) {
return time.getFullYear() + '-' + (time.getMonth() + 1) + '-' + time.getDate();
}
};app.use(render(app, {
root: path.join(__dirname, 'views'),
layout: 'layout',
viewExt: 'html',
cache: true,
debug: true,
locals: locals,
filters: filters
}));app.use(function *() {
console.time('time');
yield this.render('content', {
users: [{name: 'John'}, {name: 'Jack'}, {name: 'Tom'}]
});
console.timeEnd('time');
});app.listen(3000, function () {
console.log('listening on 3000.');
});
```
Or you can checkout the example.### settings
* root: view root directory.
* layout: global layout file, default is `layout`, set `false` to disable layout.
* viewExt: view file extension (default `html`).
* cache: cache compiled templates (default `true`).
* debug: debug flag (default `false`).
* locals: global locals, can be function type, `this` in the function is koa's ctx.
* filters: ejs custom filters.
* open: open sequence (default `<%`).
* close: close sequence (default `%>`).### $this
After version `1.4.0`, you could use `$this` in ejs template, like: `<%= $this.path %>`.
### Others
see [ejs](https://github.com/tj/ejs)
### License
MIT