在GSP中呈现HTML

时间:2014-04-01 02:37:23

标签: grails gsp

我在数据库表(博客文章)中有一些内容是可信内容,我想在屏幕上显示。此内容为HTML,并使用Prism.js进行语法突出显示的一些代码示例。由于gsp页面上的HTML econding,我需要使用raw方法按原样输出内容

${raw(post.content)}

这非常有用,除非我找到包含在我的代码示例的标记中的代码。而不是将其显示为代码,而是输出原始的html而不是我想要的。我不知何故需要对其中的文本进行编码,因为如果我不是最终得到的东西看起来像这样。

enter image description here

我知道我可以在保存时进行编码,但我已经有数百个帖子,但情况并非如此。有什么想法吗?

1 个答案:

答案 0 :(得分:6)

在我的情况下,我必须在视图中抓取原始内容

${raw(post.getEscapedContent())}

然后在域对象中我转义了代码块内的任何内容

/**
 * I will return the content of a post with the necessary html escaping. To render html in code blocks we
 * need to escape any html inside of <code></code>
 * @return String
 */
def getEscapedContent(){
    content.replaceAll(/(?ms)(<code.*?>)(.*?)(<\/code>)/) { it, open, code, close ->
        open + code.encodeAsHTML() + close
    }
}
相关问题