doulu1325 2013-12-27 21:58
浏览 229
已采纳

Golang的“ html / template”包中的HTML()函数出现问题

I am having an issue with getting the "html/template" package to parse a template properly. I am trying to parse in HTML content to a template page that I have made, however, when I try, the parsed page ends up with the escaped HTML instead of the actual code I want.

The Go documentation says I can simply use the HTML() function to convert a string into a type HTML, which is known to be safe and should be parsed in as HTML. I have made my Content field in my type Page a template.HTML, which compiles just fine, but when I use the template.HTML(content) function in line 53, I get a compiler error saying:

template.HTML undefined (type *"html/template".Template has no field or method HTML)

Using HTML(content) (without the preceding template.) results in this error:

undefined: HTML

My end goal is to have other HTML files parse into index.html and be interpreted by the browser as HTML.

Any help is appreciated.

package main

import (
    "fmt"
    "html/template"
    "io"
    "io/ioutil"
    "net/http"
    "regexp"
    "strings"
)

func staticServe() {
    http.Handle(
        "/assets/",
        http.StripPrefix(
            "/assets/",
            http.FileServer(http.Dir("assets")),
        ),
    )
}

var validPath = regexp.MustCompile("^/(|maps|documents|residents|about|source)?/$")


//  This shit is messy. Clean it up.

func servePage(res http.ResponseWriter, req *http.Request) {
    type Page struct {
        Title   string
        Content template.HTML
    }

    pathCheck := validPath.FindStringSubmatch(req.URL.Path)
    path := pathCheck[1]
    fmt.Println(path)

    if path == "" {
        path = "home"
    }

    template, err := template.ParseFiles("index.html")
    if err != nil {
        fmt.Println(err)
    }

    contentByte, err := ioutil.ReadFile(path + ".html")
    if err != nil {
        fmt.Println(err)
    }
    content := string(contentByte)

    page := Page{strings.Title(path) + " - Tucker Hills Estates", template.HTML(content)}

    template.Execute(res, page)
}

//  Seriously. Goddamn.

func serveSource(res http.ResponseWriter, req *http.Request) {
    sourceByte, err := ioutil.ReadFile("server.go")
    if err != nil {
        fmt.Println(err)
    }
    source := string(sourceByte)
    io.WriteString(res, source)
}

func main() {
    go staticServe()
    http.HandleFunc("/", servePage)
    http.HandleFunc("/source/", serveSource)
    http.ListenAndServe(":9000", nil)
}
  • 写回答

2条回答 默认 最新

  • dongmingxiang0312 2013-12-27 22:07
    关注

    Having imported "html/template" earlier, this line

    template, err := template.ParseFiles("index.html")
    

    shadows the template package, so when you do template.HTML later on, you're looking for HTML attribute on the template object, not for something called HTML in the package.

    To prevent this, change the name of your variable.

    tmpl, err := template.ParseFiles("index.html")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题