1

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:

    myapp/
    | - 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:

    script(src='scripts/test.js')
4

1 回答 1

0
app.locals.use (req, res) ->
  res.locals.script = (s) ->
    "<script type=\"text/javascript\" src=\"#{s}\"></script>"
于 2012-07-02T22:20:53.903 回答