duanhuren5581 2014-11-04 02:47
浏览 203
已采纳

Golang Martini模板在渲染Markdown时仅显示HTML

I am working on writing a simple blog in Golang using Martini, the Martini-Contrib Renderer package, and Blackfriday.

I am able to get the post into the DB and out of the DB with no issues. I even get the Body of the post out of the DB and into my struct as html however when we render the template the output is just plain text html and not looking pretty like it should.

Code is hosted here:

http://bitbucket.org/ChasingLogic/goblog

Any help would be great.

EDIT:

You can see what it's doing here:

http://chasinglogic.com/

  • 写回答

1条回答 默认 最新

  • dqtl46964 2014-11-04 05:28
    关注

    Golang templates escape variables by default. You can use template.HTML instead of string when it contains HTML and the source is trusted (which, in this instance, it seems to be).

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

    type HTML string

    HTML encapsulates a known safe HTML document fragment. It should not be used for HTML from a third-party, or HTML with unclosed tags or comments. The outputs of a sound HTML sanitizer and a template escaped by this package are fine for use with HTML.

    The way I would fix it would be by changing this

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

    to

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

    And then change

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

    to

    post.Body = template.HTML(blackfriday.MarkdownCommon([]byte(preFormatMarkdown)))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办