Jade template, how to pass concrete object to pages?

后端 未结 4 500
小鲜肉
小鲜肉 2020-12-08 06:45

I have a jade template for my node.js project. I would like to send an object to the jade template and pass it to a function inside the page (to render something).

I

相关标签:
4条回答
  • 2020-12-08 07:21

    On the JS side, you send back

    res.render(__dirname + '/pages/viz.jade', {
        vizJson: JSON.stringify(newJson),
    });
    

    On the HTML side, I have found that something like:

    JSON.parse( '!{vizJson}' )
    

    works.

    0 讨论(0)
  • 2020-12-08 07:25

    Oddly enough, for me the solution involved no calls to JSON.parse. I stringified my object on the server and just used the !{vizJson} method and got my object clientside.

    Per the docs, unescaped string interpolation: http://jade-lang.com/reference/interpolation/

    0 讨论(0)
  • 2020-12-08 07:27

    For this to work, you need to stringify on the server.

    res.render(__dirname + '/pages/viz.jade', {
        vizJson: JSON.stringify(newJson),
    });
    

    Then, as you mentioned, parse the JSON on the client.

    script
        sunburst(JSON.parse(#{vizJson}))
    

    Hope that helps!

    0 讨论(0)
  • 2020-12-08 07:37

    I hope this is going to help someone. I solved it like this:

    script
        sunburst(!{JSON.stringify(vizJson)})
    

    Notice the ! and the {...} wrapping the stringify method.

    0 讨论(0)
提交回复
热议问题