【问题标题】:EJS rendering HTMLEJS 渲染 HTML
【发布时间】:2018-12-26 17:02:24
【问题描述】:

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

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

<div class="show-post__content">

   <%= post.body %>

 </div>

post.body的内容是:'&lt;p&gt;Hello there, this is a new post.&lt;/p&gt;'

但它的渲染是这样的:

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

非常感谢,

拉夫

【问题讨论】:

    标签: html node.js express ejs


    【解决方案1】:

    说实话,一周前我也遇到过类似的问题 任务是禁用 javascript。我是用消毒剂做的。 我在插入 MongoDB 之前清理了用户信息。 所以建议你用sanitize-htmlstriptags来做。 这些包在 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();
      });
    });
    

    【讨论】:

      【解决方案2】:

      我知道了:

      <div class="show-post__content">
      
         <%- post.body %>
      
       </div>
      

      就是答案。

      谢谢你看看!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-19
        • 2012-05-08
        • 2023-03-31
        • 2016-11-01
        • 2012-11-27
        • 2015-08-19
        相关资源
        最近更新 更多