npm install think-view-nunjucks
edit config file src/config/adapter.js
, add options:
const nunjucks = require('think-view-nunjucks');
exports.view = {
type: 'nunjucks',
common: {
viewPath: path.join(think.ROOT_PATH, 'view'),
extname: '.html',
sep: '_' //seperator between controller and action
},
nunjucks: {
handle: nunjucks,
beforeRender: (env, nunjucks, config) => {}
}
}
const defaultOptions = {
autoescape: true,
watch: false,
noCache: false,
throwOnUndefined: false
};
exports.view = {
type: 'nunjucks',
nunjucks: {
handle: nunjucks,
options: {
tags: {
blockStart: '<%',
blockEnd: '%>',
variableStart: '<$',
variableEnd: '$>',
commentStart: '<#',
commentEnd: '#>'
}
},
beforeRender: (env, nunjucks, handleOptions) => {}
}
}
you can find all nunjucks support options at https://mozilla.github.io/nunjucks/api.html#configure
you can use beforeRender
method to set some env:
exports.view = {
type: 'nunjucks',
nunjucks: {
handle: nunjucks,
beforeRender: (env, nunjucks, handleOptions) => {
env.addGlobal('think', think);
env.addGlobal('JSON', JSON);
}
}
}
you can find all APIs in env
at https://mozilla.github.io/nunjucks/api.html#environment