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 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型