我在Google App Engine上使用Google Go。我将结构中的string
描述保存为datastore
,如下所示:
type Foo struct{
Bar string
}
该描述包括html标签,例如:
<a href="/">Bar</a>
我希望html template
将该描述包含在html文件中,以便将其解析为html。例如:
<html><head><title>Title</title></head>
<body>{{.Bar}}</body></html>
解析为:
<html><head><title>Title</title></head>
<body><a href="/">Bar</a></body></html>
但相反,我得到这样的东西:
<html><head><title>Title</title></head>
<body><a href="/">Bar's</a></body></html>
如何让template
正确解析string
到html链接?
答案 0 :(得分:5)
"http/template"
包会自动转义所有字符串。要解决此问题,您必须使用template.HTML
类型的值。 E.g。
import "html/template"
type Foo struct {
Bar template.HTML
}
然后在你的代码中执行类似的操作:
Foo.Bar = template.HTML(barString)