在script标签内传递一个response.render的值
以下是一个pug
片段。 我需要在chatConfig
对象中dynamic地填充user
。
script. var chatConfig = { user : 'foo', pass : 'bar', }
从我expression,我渲染
resp.render('user/index', {username:req.user});
如何将req.user
的值req.user
给script.
内部的user
密钥script.
?
Brmmmm在正确的轨道上,但是你需要让你的JavaScript使用variables作为一个对象,而不是一个variables。 您可以将variablesstring化,以使其作为JavaScript对象工作。
doctype html html(lang='en') head title Jade - var test = {prop1: "Hi there", prop2: "Another test"}; script(type='text/javascript'). var test2 = !{JSON.stringify(test)}; console.log(test2, test2.prop1, typeof test2); body h1 Jade - template engine #container.col p I'm some Jade
现场示例 ; 检查浏览器控制台/ Codepen控制台以查看console.log
语句输出的内容。
我认为这应该是这样的:
script. var chatConfig = { user : '${username}', pass : 'bar', }