🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

koa-njk

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-njk

A Koa middleware for Nunjucks

latest
Source
npmnpm
Version
1.0.6
Version published
Weekly downloads
14
75%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

koa

FAQs

Package last updated on 12 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts