Skip to content

[DEPRECATED] koa local variables middleware, supprots application local variables for templates render in application.

License

Notifications You must be signed in to change notification settings

koa-modules/koa-locals

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

[DEPRECATED] koa local variables middleware, supprots application local variables for templates render in application.

Resources

License

Stars

Watchers

Forks

Packages

No packages published