Raph117
Raph117

Reputation: 3841

EJS rendering HTML

I've got a simple blog app on Express and MongoDB, using EJS to render my data.

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>'

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?

Many thanks,

Raph

Upvotes: 0

Views: 2520

Answers (2)

Nuriddin Kudratov
Nuriddin Kudratov

Reputation: 518

To be honest, I had the similar issue a week ago Mission was to disable javascript. I did that with sanitizer. I sanitized user info before inserting in MongoDB. So recommend you to do that with sanitize-html striptags. Those packages are in npm you can download the package. 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();
  });
});

Upvotes: 5

Raph117
Raph117

Reputation: 3841

I've got it:

<div class="show-post__content">

   <%- post.body %>

 </div>

Is the answer.

Thanks if you had a look!

Upvotes: 5

Related Questions