EJS 渲染 HTML

Rap*_*117 0 html ejs node.js express

我在 Express 和 MongoDB 上有一个简单的博客应用程序,使用 EJS 来呈现我的数据。

我遇到的问题是我希望它的样式像一个段落:

<div class="show-post__content">

   <%= post.body %>

 </div>
Run Code Online (Sandbox Code Playgroud)

post.body的内容是:'<p>Hello there, this is a new post.</p>'

但它的渲染是这样的:

如果图片不起作用,它会显示出可见的括号,而不是看起来像实际的 html p 标签(如果这有意义的话)...有人知道如何解决这个问题吗?

非常感谢,

拉夫

Rap*_*117 5

我懂了:

<div class="show-post__content">

   <%- post.body %>

 </div>
Run Code Online (Sandbox Code Playgroud)

就是答案。

如果您看过的话,谢谢!


Nur*_*tov 5

老实说,一周前我也遇到过类似的问题,任务是禁用 javascript。我用消毒剂做到了这一点。在插入 MongoDB 之前,我清理了用户信息。因此建议您使用sanitize-html striptags. 这些包位于 npm 中,您可以下载该包。我希望你能解决这个问题。

var striptags = require('striptags');    
    YourCollectionName.find({}, function (err, obj) {
      if (err) {
        //handle or throw error
      }

  // For all the documents, remove html tags and save
  obj.forEach(function(ob){
    ob.body= striptags(ob.body);
    ob.save();
  });
});
Run Code Online (Sandbox Code Playgroud)