简体   繁体   English

使用coffeescript和express.js将变量传递给玉器时遇到麻烦

[英]trouble passing variables to jade using coffeescript and express.js

I'm new to node and express and working on the front end of an app that uses jade as a templating language. 我是Node的新手,他表示并致力于使用玉作为模板语言的应用程序的前端。 There is something I'm still having trouble wrapping my brain around and exhaustive googling and searching here haven't dug up the answer. 我仍然无法解决问题,在Google上进行详尽的谷歌搜索和搜索并没有找到答案。

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.coffee中定义变量,则可以将其传递给jade,例如:

    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: 进入landing.jade,它将呈现为:

    <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? 但是,如果我想在资产中的其他coffeescript文件之一中定义一个变量或对象,然后用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. 我要避免的是用一堆对象/变量弄脏我的app.coffee文件,我只想在我的资产coffee文件中定义它们,然后在用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: 值得一提的是,使用经典脚本标记将编译后的咖啡文件包含在页面顶部的layout.jade文件中:

    script(src='scripts/test.js')
app.locals.use (req, res) ->
  res.locals.script = (s) ->
    "<script type=\"text/javascript\" src=\"#{s}\"></script>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM