Go template.ExecuteTemplate include html

后端 未结 8 1963
我在风中等你
我在风中等你 2020-12-01 10:19

I have followed this tutorial: http://golang.org/doc/articles/wiki/final.go and have slightly modified it for my needs/wants. The problem is I would like to support HTML in

8条回答
  •  粉色の甜心
    2020-12-01 11:00

    Convert your []byte or string to type template.HTML (documented here)

    p.Body = template.HTML(s) // where s is a string or []byte
    

    Then, in your template, just:

    {{.Body}}
    

    It will be printed without escaping.

    EDIT

    In order to be able to include HTML in you page's body you need to change the Page type declaration:

    type Page struct {
        Title string
        Body  template.HTML
    }
    

    then assign to it.

提交回复
热议问题