[英]Passing variables to the next middleware using next() in Express.js
[英]trouble passing variables to jade using coffeescript and express.js
我是Node的新手,他表示並致力於使用玉作為模板語言的應用程序的前端。 我仍然無法解決問題,在Google上進行詳盡的谷歌搜索和搜索並沒有找到答案。
我的應用程序結構:
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
目前,如果我在app.coffee中定義變量,則可以將其傳遞給jade,例如:
app.get '/', (req, res) ->
res.render 'landing',
testvar: 'a test string'
然后我可以輸入:
p #{testvar}
進入landing.jade,它將呈現為:
<p>a test string</p>
一切都很好。 但是,如果我想在資產中的其他coffeescript文件之一中定義一個變量或對象,然后用jade渲染它,該怎么辦? 最好的方法是什么? 這可能嗎? 我已經研究過快速曝光,但似乎這並不是我想要的,或者可能我沒有正確理解它。
我要避免的是用一堆對象/變量弄臟我的app.coffee文件,我只想在我的資產coffee文件中定義它們,然后在用app.get定義路線時將它們包括在內。 值得一提的是,使用經典腳本標記將編譯后的咖啡文件包含在頁面頂部的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.