duanhuren5581 2014-11-03 18: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-03 21: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 cmake+mingw使用<mysqlx/xdevapi.h>报错
  • ¥15 eNSP中防火墙的使用
  • ¥15 不能对数据库增删改但是可以查询
  • ¥15 在触控设备上启动TabTip.exe打不开键盘界面,怎么用代码启动进程打开界面
  • ¥15 关于#mlnet#的问题:mlnet相关请求(语言-c#)
  • ¥15 lvgl7.11怎么做出文字被选中的效果
  • ¥50 如何快速查看手机目标app的主要服务器ip
  • ¥15 (标签-stm32|关键词-m3)
  • ¥15 matlab中频率调制法代码的解读
  • ¥15 ceph的对象、块、文件相关问题求解答
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部