Search code examples
node.jsexpresspug

Use a variable in a Jade include


I'm working with Jade and Express and I would like to use a variable in my include statement. For example:

app.js

app.get('/admin', function (req, res) {
  var Admin = require('./routes/admin/app').Admin;

  res.render(Admin.view, {
    title: 'Admin',
    page: 'admin'
  });
});

layout.jade

- var templates = page + '/templates/'

include templates

When I do this I get the error EBADF, Bad file descriptor 'templates.jade'

I even tried

include #{templates}

to no avail.


Solution

  • AFAIK JADE does not support dynamic including. What I suggest is to "include" outside the template, i.e.

    app.js

    app.get('/admin', function (req, res) {
        var Admin = require('./routes/admin/app').Admin;
        var page = 'admin';
    
        var templates = page + '/templates/';
    
        // render template and store the result in html variable
        res.render(templates, function(err, html) {
    
            res.render(Admin.view, {
                title: 'Admin',
                page: page,
                html: html
            });
    
        });
    
    });
    

    layout.jade

    |!{ html }