Accessing EJS variable in Javascript logic

后端 未结 6 480
不知归路
不知归路 2020-12-02 20:21

I\'m working on a Node.js app (it\'s a game). In this case, I have some code set up such that when a person visits the index and chooses a room, he gets redirected to the pr

6条回答
  •  萌比男神i
    2020-12-02 20:54

    I had the same problem. I needed to use the data not for just rendering the page, but in my js script. Because the page is just string when rendered, you have to turn the data in a string, then parse it again in js. In my case my data was a JSON array, so:

    
    

    Single quotes are there to not be mixed with double-quotes of stringify. Also from ejs docs:

    "<%- Outputs the unescaped value into the template"

    The same can be done for arrays. Just concat the array then split again.

提交回复
热议问题