简体   繁体   English

渲染从SailsJS控制器返回的HTML内容

[英]Render HTML content returned from SailsJS Controller

I am new to SailsJS and trying it out. 我是SailsJS的新手,请尝试一下。 I want to save some HTML content to database, retrieve and render it as HTML when requested. 我想将一些HTML内容保存到数据库,在请求时将其检索并呈现为HTML。 I have generated a sails model and a controller to accomplish this. 我已经生成了一个Sails模型和一个控制器来完成此任务。

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. 显然,当我在视图中使用它时,html标记会转义并呈现为文本。

<%= 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. 我猜想Sails / EJS在渲染之前会将HTML转义。 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? 还是可以让我知道其他选项来呈现从控制器返回的HTML内容? 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. 实际上,对EJS的深入研究(到目前为止,我只是从SailsJS看)可以帮助我解决这一问题。 Using <%- code %> tag actually fixed my issue. 使用<%-code%>标签实际上解决了我的问题。 Hope this will help somebody in future. 希望这对将来有帮助。

This actually helped me. 这实际上帮助了我。

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

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