简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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