32

I am passing in a session variable from my mongodb session store in an Express for Node.js web app like this:

exports.dashboard = function(req, res){
    res.render('dashboard', {pref: req.session.layoutpref});
}

Then, in my Jade file I'm trying to assign the value of pref to the css link like this, but I'm getting a syntax error:

head
        title #{title}
        link(rel='stylesheet', href='/stylesheets/' + #{pref} + '.css')

I'm almost certain the problem is in my concatenation of pref into the location of the css file to be used. Any ideas how to fix this?

gjw80
  • 1,058
  • 4
  • 18
  • 36

2 Answers2

68

use #{} notation if you want to interpolate a variable in the contents of an element. you can just use the variable names straight up if you want to use them in attributes.

link(rel='stylesheet', href='/stylesheets/' + pref + '.css')

equivalent:

link(rel='stylesheet', href='/stylesheets/' + locals.pref + '.css')

when to use #{}:

a(href='/stylesheets/' + locals.pref + '.css') View the stylesheet at #{pref}
Plato
  • 10,812
  • 2
  • 41
  • 61
5

Jade files are compiled in Node.js env.

Node.js (from v4.0.0) supports template literals, so

link(rel='stylesheet', href=`/stylesheets/${pref}.css`)

equivalent:

link(rel='stylesheet', href='/stylesheets/' + pref + '.css')