queckezz / koa-views

Template rendering middleware for koa (hbs, swig, pug, anything! :sparkles:)
MIT License
710 stars 87 forks source link

how to define Customer filter in nunjucks ? #94

Closed wuchuguang closed 7 years ago

wuchuguang commented 7 years ago

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

int64ago commented 7 years ago
module.exports = env;
module.exports = nunjucks;//or this

Why exported twice here ?

wuchuguang commented 7 years ago

when module.exports = env report nunjucks error

when module.exports = nunjucks report filter error

int64ago commented 7 years ago

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