Concatenating a variable + string in Jade file

gjw80 picture gjw80 · Oct 15, 2013 · Viewed 37.4k times · Source

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?

Answer

Plato picture Plato · Oct 15, 2013

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}