# koa-locals

> Supprots application local variables for templates render in application.

Latest version **0.3.0** (published 2014-04-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-locals
pnpm add koa-locals
yarn add koa-locals
bun add koa-locals
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2014-04-22 |
| First published | 2014-03-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 5 |
| Author | fundon |
| Maintainers | fundon |
| Keywords | koa, locals, variables, render, view |

## Links

- npm: https://www.npmjs.com/package/koa-locals
- Repository: http://github.com/fundon/koa-locals
- Homepage: https://github.com/fundon/koa-locals
- Issues: https://github.com/fundon/koa-locals/issues
- npm.io page: https://npm.io/package/koa-locals

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^0.7.4
- [utils-merge](https://npm.io/package/utils-merge.md) ^1.0.0

## Recent versions

- 0.3.0 (latest) — 2014-04-22
- 0.2.0 — 2014-03-22
- 0.1.3 — 2014-03-17
- 0.1.2 — 2014-03-17
- 0.1.1 — 2014-03-17
- 0.1.0 — 2014-03-17

## README

# koa-locals

Supports application local variables for templates render in application.

  [![Build Status](https://travis-ci.org/fundon/koa-locals.svg)](https://travis-ci.org/fundon/koa-locals)

### Usage

#### Install

```
npm install koa-locals
```

#### API

```js
require('koa-locals')(app, locals);
```

##### this.locals or this.response.locals
Lazily creates a locals object on every request.
```js
// get locals
var locals = this.locals;

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

#### Example

```js
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

---
_Source: https://npm.io/package/koa-locals · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
