admin管理员组文章数量:1420078
I use Node.js with Express and EJS and I want to pass html tags in a string to the browser like this:
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
<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
I use Node.js with Express and EJS and I want to pass html tags in a string to the browser like this:
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
<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
-
1
Split it into two attributes. Text and name. Like this
{ text: " You have a message from", username: "John"}
. Then just use the two attributes in the template. <p><%= message.text %> <b><%= message.username %> </b></p> – magnudae Commented Jan 18, 2016 at 15:10 - I will have different kind of messages: – jfjobidon Commented Jan 18, 2016 at 15:20
- you have a new <a href="linkToMessage">message</a> from <b>user111<b> You have a <a href="linkToRequest">request</a> to access your wallet The wallet <a href="linkToWallet">ABC</a> will needs more funds that's why I would like to construct the text before sending it to the template – jfjobidon Commented Jan 18, 2016 at 15:28
1 Answer
Reset to default 6To render raw html with ejs, use <%- 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
本文标签: javascriptHow to pass html tags in a string with nodejsexpress and ejsStack Overflow
版权声明:本文标题:javascript - How to pass html tags in a string with node.js, express and ejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745319834a2653327.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论