Golang Martini模板在呈现Markdown时只显示HTML

时间:2014-11-04 02:47:13

标签: html templates go markdown martini

我正在使用Martini,Martini-Contrib Renderer软件包和Blackfriday在Golang上写一个简单的博客。

我可以将帖子放到数据库中,而不会出现问题。我甚至从数据库中获取了帖子的主体,并将其作为html输入到我的结构中,但是当我们渲染模板时,输出只是纯文本html而不是看起来应该是这样。

代码托管在这里:

http://bitbucket.org/ChasingLogic/goblog

任何帮助都会很棒。

编辑:

你可以看到它在这里做了什么:

http://chasinglogic.com/

1 个答案:

答案 0 :(得分:2)

默认情况下Golang模板转义变量。当它包含HTML并且信任源(在这种情况下,它似乎是)时,您可以使用template.HTML而不是string

http://golang.org/pkg/html/template/#HTML

  

输入HTML字符串

     

HTML封装了一个已知的安全HTML文档片段。它不应该用于来自第三方的HTML,也不能用于带有未关闭标签或注释的HTML。声音HTML清理程序的输出和此程序包转义的模板可以与HTML一起使用。

我要解决的方法是改变这个

type Post struct {
  Title  string
  Body   string
  Author string
  Date   string
}

type Post struct {
  Title  string
  Body   template.HTML
  Author string
  Date   string
}

然后改变

post.Body = string(blackfriday.MarkdownCommon([]byte(preFormatMarkdown)))

post.Body = template.HTML(blackfriday.MarkdownCommon([]byte(preFormatMarkdown)))