英文:
Trouble with the HTML() function in Golang's "html/template" package
问题
我遇到了一个问题,无法正确解析"html/template"
包中的模板。我试图将HTML内容解析到我创建的模板页面中,但是当我尝试时,解析后的页面显示的是转义后的HTML,而不是我想要的实际代码。
Go文档中说我可以简单地使用HTML()
函数将字符串转换为type HTML
,这被认为是安全的,并且应该被解析为HTML。我已经将我的type Page
中的Content
字段设置为template.HTML
,这样编译就没有问题,但是当我在第53行使用template.HTML(content)
函数时,我得到一个编译器错误,提示:
template.HTML未定义(类型*"html/template".Template没有HTML字段或方法)
在不带前缀template.
的情况下使用HTML(content)
会导致以下错误:
未定义:HTML
我的最终目标是让其他HTML文件解析为index.html
,并由浏览器解释为HTML。
感谢任何帮助。
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)?/$")
// 这段代码有点乱,请整理一下。
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)
}
// 真的,该整理了。
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)
}
英文:
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)
}
答案1
得分: 8
在之前导入了"html/template"
包后,这一行代码:
template, err := template.ParseFiles("index.html")
遮蔽了template
包,所以当你后面使用template.HTML
时,你是在寻找模板对象上的HTML
属性,而不是寻找包中的名为HTML
的东西。
为了避免这种情况,你需要更改变量的名称:
tmpl, err := template.ParseFiles("index.html")
英文:
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")
答案2
得分: 0
你正在使用一个名为template的变量来遮蔽你的包。最简单的修复方法可能是将变量名更改为tmpl或其他名称。
英文:
You are shadowing your package with a variable called template. The easiest fix is probably to change your variable name to tmpl or something.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论