My attempt to implement a custom filter with nunjucks, as per the documentation, resulted in an error message:
Error: filter not found: my_filter_here
Here are the configurations I have:
index.js
const express = require('express');
const nunjucks = require('nunjucks');
const port = 3000;
const app = express();
nunjucks.configure('views', {
autoescape: true,
express: app
})
app.set('view engine', 'html');
const env = new nunjucks.Environment();
env.addFilter('is_undefined', function(obj) {
return typeof obj === 'undefined';
});
For template rendering, I am using
res.render('register.html', {errors: errors});
In my template, I have set it up like this:
{% if errors|is_undefined %}
// do something
{% endif %}
The error I am facing is: Error: filter not found: is_undefined
Some suggestions on Stack Overflow mentioned using env.render()
instead of res.render
, but changing it caused my HTML page not to render. Any solutions to correct this issue?
Thank you in advance. :)