• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

strawbrary/koa-nunjucks-2: Lightweight Koa middleware for Nunjucks

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

strawbrary/koa-nunjucks-2

开源软件地址:

https://github.com/strawbrary/koa-nunjucks-2

开源编程语言:

JavaScript 95.0%

开源软件介绍:

koa-nunjucks-2

Lightweight Koa middleware for Nunjucks.

NPM version Build status License Dependency status

Installation

npm install --save koa-nunjucks-2

NOTE: v3 requires Koa 2 or later. If you're using Koa 1, use v2 of this package.

Usage

Example

const Koa = require('koa');
const app = new Koa();
const koaNunjucks = require('koa-nunjucks-2');
const path = require('path');

app.use(koaNunjucks({
  ext: 'html',
  path: path.join(__dirname, 'views'),
  nunjucksConfig: {
    trimBlocks: true
  }
}));

app.use(async (ctx) => {
  await ctx.render('home', {double: 'rainbow'});
});

Config Options

  • ext (default: 'njk'): Extension that will be automatically appended to the file name in ctx.render calls. Set to a falsy value to disable.
  • path (default: current directory): Path to the templates. Also supports passing an array of paths.
  • writeResponse (default: true): If true, writes the rendered output to response.body.
  • functionName (default: 'render'): The name of the function that will be called to render the template.
  • nunjucksConfig: Object of Nunjucks config options.
  • configureEnvironment: A function to modify the Nunjucks environment. See the [Extending Nunjucks](#Extending Nunjucks) section below for usage.

Global Template Variables

Use ctx.state to make a variable available in all templates.

Extending Nunjucks

Use the configureEnvironment config option to define a function which will receive a Nunjucks Environment as its argument. This allows you to define custom filters, extensions etc.

app.use(koaNunjucks({
  ext: 'html',
  path: path.join(__dirname, 'views'),
  configureEnvironment: (env) => {
    env.addFilter('shorten', (str, count) => {
      return str.slice(0, count || 5);
    });
  }
}));

License

MIT




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap