[英]EJS template for express.js app: transferring data into variable
I want to transfer data from my node.js (express) app to a variable in the <script>
on the page.我想将数据从我的 node.js(快速)应用程序传输到页面上<script>
中的变量。
On the server-side I have在服务器端我有
let tmp = JSON.stringify(await f(i));
console.log(tmp); //correct data [{"id":"729199920929701988","a ...}]
res.render('r.ejs', {"guilds": tmp});
Then in r.ejs
I have然后在r.ejs
我有
<input type='hidden' id='getter' value="<%= guilds %>">
<script>
console.log(document.getElementById("getter").value); //correct value: [{"id":"729199920929701988","...}]
let v = "<%= guilds %>";
console.log(v);
console.log("<%= guilds %>"); //dumb value with " changed to special symbols: [{"id":"729199920929701988","...}]
</script>
The way with making an extra tag does not look like a good way in terms of performance.就性能而言,制作额外标签的方法看起来并不是一个好方法。 At least it makes the browser create one more element which takes time, especially if more than one element like this will be needed.至少它使浏览器创建一个需要时间的元素,特别是如果需要多个这样的元素。
I would like to get the data directly in the <script>
tag.我想直接在<script>
标签中获取数据。 There should be a way to avoid changing "
to "
when getting the value directly in the <script>
tag (in fact, I do not understand why does that even happen). Can anyone help with that?当直接在<script>
标签中获取值时,应该有一种方法可以避免将"
更改为"
(事实上,我不明白为什么会发生这种情况)。有人可以帮忙吗?
Just use let v = <%- guilds %>;
只需使用let v = <%- guilds %>;
instead of let v ="<%= guilds %>";
而不是let v ="<%= guilds %>";
to tell ejs you need unescaped data告诉 ejs 你需要未转义的数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.