Have been trying to implement nunjucks with my express app but getting following error in browser.
{
"message": "Failed to lookup view \"index\" in views directory \"/home/gblp074/Desktop/projects/API/fsapi/app/views\"",
"error": {
"view": {
"defaultEngine": "nunjucks",
"ext": ".nunjucks",
"name": "index",
"root": "/home/gblp074/Desktop/projects/API/fsapi/app/views"
}
}
}
Did you try using the template name "index.nunjucks" instead of "index"? I'm not sure if we support the 'ext' option. If that doesn't work, could you provide a sample of how you're setting up nunjucks?
I did some modification but it still doesn't work. Here's snippet of some of my setup. Not sure if that's perfect!
router/index.js
router.get('/', function(req, res, next) {
res.render('index',{
title: 'Index title',
csrfToken: req.csrfToken(),
chunkOutput: chunkOutput['main.js']});
});
app.js
var nunjucks = require('nunjucks');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'templates'));
app.set('view engine', 'html');
nunjucks.configure('views', {
autoescape: true,
express: app
});
However now I'm getting following error while I run app, I'm getting following error on index
{
"message": "template not found: index.html",
"error": {
}
}
// no need to set this with express here, nunjucks does it's own path handling
app.set('views', path.join(__dirname, 'templates'));
app.set('view engine', 'html');
// the first argument to nunjucks is the path to the folder
// in this case you're telling it to use ./views
nunjucks.configure('views', {
autoescape: true,
express: app
});
this should work
app.set('view engine', 'html');
nunjucks.configure(path.join(__dirname, 'templates'), {
autoescape: true,
express: app
});
See the api docs for using configure: https://mozilla.github.io/nunjucks/api.html#configure
Woah! That worked for me.... Thanks a ton man..
Also confirming that this works. Thank You @devoidfury!
Most helpful comment
this should work
See the api docs for using configure: https://mozilla.github.io/nunjucks/api.html#configure