Release 2.3.0
nunjucks view plugin for egg.
$ npm i egg-view-nunjucks --save
// {app_root}/config/plugin.js exports.nunjucks = { enable: true, package: 'egg-view-nunjucks', };
Set mapping in config
// {app_root}/config/config.default.js exports.view = { defaultViewEngine: 'nunjucks', mapping: { '.nj': 'nunjucks', }, };
Render in controller
// {app_root}/app/controller/test.js class TestController extends Controller { async list() { const ctx = this.ctx; // ctx.body = await ctx.renderString('{{ name }}', { name: 'local' }); // not need to assign `ctx.render` to `ctx.body` // https://github.com/mozilla/nunjucks/blob/6f3e4a36a71cfd59ddc8c1fc5dcd77b8c24d83f3/nunjucks/src/environment.js#L318 await ctx.render('test.nj', { name: 'view test' }, { path: '***' }); } }
escape
helper.escape
egg-security
app/extend/filter.js
// {app_root}/app/extend/filter.js exports.hello = name => `hi, ${name}`; // so you could use it at template // {app_root}/app/controller/test.js class TestController extends Controller { async list() { const ctx = this.ctx; ctx.body = await ctx.renderString('{{ name | hello }}', { name: 'egg' }, { path: '***' }); }; }
you can extend custom tag like this:
// {app_root}/app.js const markdown = require('nunjucks-markdown'); const marked = require('marked'); module.exports = app => { markdown.register(app.nunjucks, marked); };
see egg-security
_csrf
nonce
helper/ctx/request
helper.shtml('<div></div>')
helper.upper('test')
helper.shtml/surl/sjs/escape
safe
app.nunjucks
app.nunjucks.cleanCache(fullPath/tplName)
see config/config.default.js for more detail.
Please open an issue here.
MIT
版权所有:中国计算机学会技术支持:开源发展技术委员会 京ICP备13000930号-9 京公网安备 11010802032778号
egg-view-nunjucks
nunjucks view plugin for egg.
Install
Usage
Set mapping in config
Render in controller
Feature
Filter
escapefilter is replaced byhelper.escapewhich is provided byegg-securityfor better performanceapp/extend/filter.js, then they will be injected automatically to nunjucksTag
you can extend custom tag like this:
Security
see egg-security
_csrfattr to form fieldnonceattr to script tagHelper / Locals
helper/ctx/requestin template, such ashelper.shtml('<div></div>')helper.upper('test')helper.shtml/surl/sjs/escapeis auto wrapped withsafeMore
app.nunjucks- nunjucks environmentapp.nunjucks.cleanCache(fullPath/tplName)to easy clean cache, can use with custom egg-watcherConfiguration
see config/config.default.js for more detail.
Questions & Suggestions
Please open an issue here.
License
MIT