dpv21589 2017-08-02 19:23
浏览 34

Golang中的模板未呈现

I am working in a Golang project, currently am trying to render some templates but the HTML code of the template is being printed in the browser. My project structure is:

|-app
|---models
|---controller
|---templates
|------index.gohtml
|-main.go

Then, the code that I have in one controller, to render the template is:

func Index(w http.ResponseWriter, req *http.Request, ps httprouter.Params){
    var tpl *template.Template
    tpl = template.Must(template.ParseGlob("app/templates/*.gohtml"))

    err := tpl.ExecuteTemplate(w, "index.gohtml", nil)

    if(err!=nil){
        fmt.Println("error:",err.Error())
    }

However I am not getting any error. But in the browser the code that I have inside index.gohtml is being printed and not rendered }

  • 写回答

1条回答 默认 最新

  • dsjhejw3232 2017-08-03 17:23
    关注

    checkout net/http/sniff.go. It shows how net/http infers MIME types.

    For Html your content can start with :

    • <!DOCTYPE HTML
    • <HTML
    • <HEAD
    • <SCRIPT
    • <IFRAME
    • <H1
    • <DIV
    • <FONT
    • <TABLE
    • <A
    • <STYLE
    • <TITLE
    • <B
    • <BODY
    • <BR
    • <P
    • <!--
    评论

报告相同问题?