Search code examples
javascriptnode.jsunderscore.jspugunderscore.js-templating

Accessing JADE variable in Underscore template


I am passing current user id (userid of logged in user) like

app.get('/home', isLoggedIn, function(req, res) {
        res.render('home.jade', {
            currentUser : req.user.id 
        });
    });

Now this means my home.jade file has access to currentUser variable.

I am able to access this in Jade like

p= currentUser

But I want to access this variable inside by Underscore template inside

script(type='text/template')

How can I do that?


Solution

  • If it's inline then you should be able to return your variable using #{}.

    So you'd want

    script(type='text/template').
        var template = _.template("<b><%- value %></b>");
        template({value: "#{currentuser}"});