简体   繁体   English

EJS呈现HTML

[英]EJS rendering HTML

I've got a simple blog app on Express and MongoDB, using EJS to render my data. 我在Express和MongoDB上有一个简单的博客应用程序,使用EJS呈现数据。

The problem I have is I want this to be styled like a paragraph: 我的问题是我希望将此样式设置为一个段落:

<div class="show-post__content">

   <%= post.body %>

 </div>

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

but it's rendering like this: 但是它是这样渲染的:

If the picture doesn't work, it's showing up with the brackets visible, rather than looking like an actual html p tag if that makes any sense... Does anyone know how to get around this? 如果图片不起作用,则显示为带有括号,而不是看起来像实际的html p标签(如果有任何意义的话)。有人知道如何解决此问题吗?

Many thanks, 非常感谢,

Raph 拉夫

I've got it: 我懂了:

<div class="show-post__content">

   <%- post.body %>

 </div>

Is the answer. 是答案。

Thanks if you had a look! 谢谢,如果你看看!

To be honest, I had the similar issue a week ago Mission was to disable javascript. 老实说,一周前我遇到了类似的问题,任务是禁用javascript。 I did that with sanitizer. 我用消毒剂做到了。 I sanitized user info before inserting in MongoDB. 在插入MongoDB之前,我先清理了用户信息。 So recommend you to do that with sanitize-html striptags . 因此,建议您使用sanitize-html striptags Those packages are in npm you can download the package. 这些软件包位于npm中,您可以下载该软件包。 I hope you will solve the problem. 希望您能解决问题。

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();
  });
});

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

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