Vick
Vick

Reputation: 41

Render HTML content returned from SailsJS Controller

I am new to SailsJS and trying it out. I want to save some HTML content to database, retrieve and render it as HTML when requested. I have generated a sails model and a controller to accomplish this.

Model:

attributes: {
    html: "string"
}

Controller action:

find: function(req, res) {
    Item.findOne({
        'id': req.params['id']
    }, function(err, item) {
        console.log(item.html);

        res.view({
            item: item
        });
    })
}

Apparently, when I use it in view, html tags are escaped and rendered as text.

<%= item.html %>

Actual:
&lt;p&gt;Sample HTML content&lt;/p&gt;

Expected:
<p>Sample HTML content</p>

I am guessing either Sails/EJS escaping the HTML before rendering. I am wondering if there is an option to override this behavior? or can you please let me know other options to render HTML content returned from controller? Greatly appreciate it!

Upvotes: 0

Views: 1480

Answers (1)

Tory Adams
Tory Adams

Reputation: 61

Just posting the answer vick gave.

Actually, little more digging into EJS [I was looking from SailsJS only so far] helped me resolve this. Using <%- code %> tag actually fixed my issue. Hope this will help somebody in future.

This actually helped me.

Upvotes: 3

Related Questions