queckezz/koa-views

how to define Customer filter in nunjucks ?

wuchuguang opened this issue · 3 comments

filter.js

import nunjucks from 'nunjucks';

let env = new nunjucks.Environment();
env.addFilter('sex', function(str){
    switch (str){
        case 1:
            return "男";
        default :
            return "女";
    }
});


module.exports = env;
module.exports = nunjucks;//or this


import filter from './filter';
app.use(views(__dirname + '/views', { options: {
    nunjucksEnv:filter,
    settings: {
        views: path.join(__dirname, 'views')
    }
},map: {html: 'nunjucks' }}))

but not validate

module.exports = env;
module.exports = nunjucks;//or this

Why exported twice here ?

when module.exports = env report nunjucks error

when module.exports = nunjucks report filter error

I write an example for your case, hope it helps.