0
votes

I'm beginning to use Node.js and i've choosen my template engine, which is ejs. My app uses socket.io and on a particular request, it tries to render an ejs file and emit back to the client. The ejs file is rendered beautifully when its a single ejs file without include, but when i try to include another ejs file, it crashes with the following message: "Error: filename option is required for includes". Note, that

  app.get('/', function(req, res){
    res.render(__dirname+'/views/main/index',{});
   });

works just fine, even if index.ejs contains <% include something %>. My other code partial, which is not working if index.ejs has include (without include it works):

io.on('connection', function(socket){
    fs.readFile('/views/main/index', 'utf8', function (err, template) {
       var content = ejs.render(template,{
                            data:data
                            },function(err,result){
                            console.log(result);
    });
});

Here is a example ejs:

<% include list_header %> 
<span>random content</span>
1

1 Answers

1
votes

When you want to use includes with ejs.render() you also need to pass the filename option:

var content = ejs.render(template, {
  data     : data,
  filename : '/views/main/index'
});

(filename needs to be the full path of your main template; in your code you're using /views/main/index so I used that)

If you regularly need to render the same template I would advise using ejs.compile().