简体   繁体   English

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

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

I use Node.js with Express and EJS and I want to pass html tags in a string to the browser like this:我将 Node.js 与 Express 和 EJS 一起使用,我想将字符串中的 html 标记传递给浏览器,如下所示:

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

Later in my code:稍后在我的代码中:

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

And in my html template, I have something like在我的 html 模板中,我有类似的东西

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

The problem: the browser displays the text with the tags like <b>John</b> instead of John问题:浏览器显示带有标签的文本,例如<b>John</b>而不是John

To render raw html with ejs, use <%- your_var %> .要使用 ejs 呈现原始 html,请使用<%- your_var %> In your case:在你的情况下:

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

It's the same to render partial views.. etc.. give it a try渲染部分视图是一样的..等..试一试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM