如何使用 node.js、express 和 ejs 在字符串中传递 html 标签

How to pass html tags in a string with node.js, express and ejs

我将 Node.js 与 Express 和 EJS 一起使用,并且 我想像这样将字符串中的 html 标签传递给浏览器:

listRequests.forEach(function(key) {
    messages.push("You have a message from <b>" + key.username + "</b>");
});

稍后在我的代码中:

res.render('/wallets', {
              messages     : messages,
              ...
           });

在我的 html 模板中,我有类似

的东西
<h2>Messages</h2>
<% messages.forEach(function(message) { %>
<p><%= message %></p>
<% }); %>

问题:浏览器显示带有 <b>John</b> 标签的文本,而不是 John

要使用 ejs 渲染原始 html,请使用 <%- your_var %>。 你的情况:

<h2>Messages</h2>
<% messages.forEach(function(message) { %>
<p><%- message %></p>
<% }); %>

渲染部分视图是一样的..等..试一试