[DEPRECATED] koa local variables middleware, supprots application local variables for templates render in application.
JavaScript
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example
test
.gitignore
.travis.yml
History.md
LICENSE
Makefile
README.md
index.js
package.json

README.md

koa-locals Build Status

Supports application local variables for templates render in application.

Please use this.state instead of this.locals, Koa added this.state start from 0.14.0.

NPM

Usage

Install

npm install koa-locals

API

require('koa-locals')(app, locals);
this.locals or this.response.locals

Lazily creates a locals object on every request.

// get locals
var locals = this.locals;

// set locals
this.locals.app = 'github';
this.locals = {};

Example

var koa = require('koa');
var locals = require('../');
var csrf = require('koa-csrf');
var render = require('koa-swig');
var session = require('koa-session');
var app = koa();

app.keys = ['key'];

locals(app, {
  name: 'koa app'
});

csrf(app);
app.use(session())
app.use(function *(next) {
  this.locals._csrf = this.csrf;
  yield next;
});

render(app, {
  root: __dirname,
  ext: 'html',
  locals: {
    language: 'zh-cn'
  }
});

app.use(function *() {
  yield this.render('index', {
    username: 'fundon'
  });
});

app.listen(2333);

License

MIT