Golang GAE - HTML模板没有正确地将链接插入网页

时间:2012-11-03 22:25:44

标签: html google-app-engine parsing templates go

我在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>&lt;a href=&#34;/&#34;&gt;Bar&#39;s&lt;/a&gt;</body></html>

如何让template正确解析string到html链接?

1 个答案:

答案 0 :(得分:5)

"http/template"包会自动转义所有字符串。要解决此问题,您必须使用template.HTML类型的值。 E.g。

import "html/template"

type Foo struct {
    Bar template.HTML
}

然后在你的代码中执行类似的操作:

Foo.Bar = template.HTML(barString)