9

I am trying to use Nunjucks as a template engine from Express. I did this:

var express = require('express');
var nunjucks = require('nunjucks');
var path = require('path');
var bodyParser = require('body-parser');
var load = require('express-load');
var fs = require("fs");

var app = express();
app.set('views', path.join(__dirname, 'views'));

var env = nunjucks.configure(app.get('views'), {
    autoescape: true,
    express:    app 
});

app.set('view engine', 'html');

But above code gives this error:

Template render error: compileExtends: cannot extend multiple times
   at Error.exports.TemplateError (C:\my\nodejs\projects\\node_modules\nunjucks\src\lib.js:49:19)
   at Object.extend.fail (C:\my\nodejs\projects\\node_modules\nunjucks\src\compiler.js:49:15)
   at Object.extend.compileExtends (C:\my\nodejs\projects\\node_modules\nunjucks\src\compiler.js:983:18)
   at Object.extend.compile (C:\my\nodejs\projects\\node_modules\nunjucks\src\compiler.js:1083:22)
   at Object.extend._compileChildren (C:\my\nodejs\projects\\node_modules\nunjucks\src\compiler.js:139:18)
   at Object.extend.compileRoot (C:\my\nodejs\projects\\node_modules\nunjucks\src\compiler.js:1050:14)
   at Object.extend.compile (C:\my\nodejs\projects\\node_modules\nunjucks\src\compiler.js:1083:22)
   at Object.module.exports.compile (C:\my\nodejs\projects\\node_modules\nunjucks\src\compiler.js:1118:11)
   at Obj.extend._compile (C:\my\nodejs\projects\\node_modules\nunjucks\src\environment.js:414:35)
   at Obj.extend.compile (C:\my\nodejs\projects\\node_modules\nunjucks\src\environment.js:403:18) 

Please tell me how to fix this ?

user272735
  • 10,473
  • 9
  • 65
  • 96
user007
  • 3,203
  • 13
  • 46
  • 77

5 Answers5

15

I used it like this:

nunjucks.configure('views', {
    express: app,
    autoescape: true
});
app.set('view engine', 'html');

Worked out for me. Got it from the nunjucks website: https://mozilla.github.io/nunjucks/getting-started.html

thepanuto
  • 783
  • 5
  • 17
8

I would like to add my solution. I came across this same problem since the express generator does not have support for the nunjucks templating engine. As mentioned, you should import the express and nunjucks dependencies and then configure nunjucks.

const app = express();
const nunjucks = require('nunjucks');
nunjucks.configure('views', {
  autoescape: true,
  express: app
});

Next, you need to decide how you want to render your templates. You have the option of responding to HTTP requests by rending a simple string or file. However, assuming your project may grow at some point I believe it's better to take advantage of routing and middleware provided by express. Using the express generator, express provides two folders, views, and routes. Within the app.js file are two methods for responding to incoming requests.

app.use('/', indexRouter);
app.use('/users', usersRouter);

Where indexRouter and usersRouter are both defined as follows:

const indexRouter = require('./routes/index');
const usersRouter = require('./routes/users');

This is telling express, when a request for "/" comes in, handle it with the index.js file in the routes folder. In the same manner, when a request for "/users" comes in, handle it with the users.js file in the routes folder. Since we are just trying to implement the nunjucks templating engine we can leave those lines as they are. It is important to understand that the index.js file in the routes folder will utilize the express Router, prepare the response, and render a file in the views folder with the same name. This means we need to create a file in the views folder called index - in order for the names to match.

The convention in the Nunjucks community is to use the njk extension. So, we will use index.njk as the name of our root page. Lastly, we need to let express know that it should be expecting the njk extension. This is because the njk extension is just a convention. We could just as easily use .html and as long as we have nunjucks configured and tell express to expect html. In app.js:

app.set('view engine', 'njk');

Now we can use the nunjucks templating engine and it will be compiled to HTML. I recommend creating a layout file and using nunjucks {% block content %} code-goes-here {% endblock %} to create reusable components that can be inherited via Nunjuck's template inheritance, i.e. {% extends "layout.njk" %}

Nunjucks is an incredibly powerful templating engine with a robust set of feature. In my opinion, it is the most underrated templating engine. It's easy to use, extremely powerful, and extensible.

For more information regarding template inheritance, this is a helpful link from jinja2, from which nunjucks was ported, click me!

If you want to see an example app I set up with Express 4, Nunjucks, and Winston for logging, you can find it on my GitHub.

Frederick John
  • 517
  • 2
  • 6
  • 14
3

This is like a basic configuration using express js, you could do something like this:

const express = require('express');
const nunjucks = require('nunjucks');
const app = express();

nunjucks.configure('views',{
    autoescape:true,
    express:app
});

app.set('views','./views'); // <--Path to your views folder

app.get('/',(request,response)=>{
    response.render('some-view.html');
});

for more references take a look at the official documentation

pedrommuller
  • 15,741
  • 10
  • 76
  • 126
1

if you want to place your templates folder on ${pwd}/teampltes

do like below.

//import
const path = require('path')

const express = require('express')
const nunjucks = require('nunjucks')


//init
const app = express()

nunjucks.configure(path.join(__dirname, 'templates'), {
    autoescape: true,
    express: app
})
JaeIL Ryu
  • 159
  • 10
1

I just setup nunjucks using the default way:

const express = require('express');
const nunjucks = require('nunjucks');

const app = express();
app.engine('html', nunjucks.render);
app.set('view engine','html');
app.set('views', globals.viewsDirectory);

"express": "4.16.4", "nunjucks": "^3.2.3",

Alex Egli
  • 1,884
  • 2
  • 24
  • 43