Take the 2-minute tour ×

I'm new to node and express and working on the front end of an app that uses jade as a templating language. There is something I'm still having trouble wrapping my brain around and exhaustive googling and searching here haven't dug up the answer.

My app structure:

    | - app.coffee
    | - config.coffee
    | - assets/ 
            | - scripts
                    my coffeescript files   (test.coffee)           
            | - stylesheets
                    my sass files
    | - public/ 
            | - images
            | - scripts
                    my coffeescript compiled as js (test.js)
            | - css
                    my sass files compiled as css
    | - views/ 
            | - jade files, layouts, partials etc

Currently I'm able to pass variables to jade if I define them in app.coffee, for example if I do:

    app.get '/', (req, res) ->
      res.render 'landing',
        testvar: 'a test string'

Then I can put:

    p #{testvar}

Into landing.jade and it will render as:

    <p>a test string</p>

That's all well and good. But what if I want to define a variable or object in one of my other coffeescript files located in assets, and then have it rendered by jade? What is the best way to go about this? Is this possible? I've looked into express-expose, but it seems this isn't quite what I'm looking for, or maybe I'm not understanding it correctly.

What I'm trying to avoid is dirtying up my app.coffee file with a bunch of object/variables, I'd rather just define them in my assets coffee files, then include them when I define the route with app.get. It's worth mentioning that the compiled coffee files are included in my layout.jade file at the top of the page using a classic script tag:

share|improve this question
same issue. I'm trying to add variables to the myfile.cofeee. Did you figure out a solution? –  Alexis Dec 6 '12 at 0:04

1 Answer 1

app.locals.use (req, res) ->
  res.locals.script = (s) ->
    "<script type=\"text/javascript\" src=\"#{s}\"></script>"
share|improve this answer
I don't understand this answer...how does this help? –  ryanrogalski Nov 26 '12 at 2:17

Your Answer


