1.0.6 • Published 6 years ago

koa-njk v1.0.6

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

koa-njk

NPM version

A Koa middleware for Nunjucks. Koa2 的一个 Nunjucks 的中间件。

Installation

npm install koa-njk

Usage

const koaNjk = require('koa-njk');

app.use(
    /**
     * nunjucks 中间件
     *
     * @param {string} path 模板根目录路径。root directory of views.
     * @param {string} [ext='.html'] 模板扩展名。extension of views'.
     * @param {any} [config={}] nunjucks 配置. Nunjucks' config.
     * @param {Function} [fn=env => {}] 对于 Environment 的自定义扩展回调。extension callback to Environment instance.
     * @returns {Function}
     */
    koaNjk(path.join(__dirname, 'views'), '.html', {
        autoescape: true,
        ...
    }, env => {
        // add filters and extensions here
    })
);

app.use(async (ctx) => {
    /**
     * Renders the template with the optional context hash.
     * 使用数据渲染模板。
     *
     * @param {string} view name of a template. 模板的名称。
     * @param {any} [context] data in tpl. 模板使用的数据。
     */
    await ctx.render('index', {title: 'koa-njk'});
});

Nunjucks' config

http://mozilla.github.io/nunjucks/api.html#configure

1.0.6

6 years ago

1.0.5

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago