2012-11-03 55 views
4

我在Google App Engine上使用Google Go。我现在的储蓄结构中的string描述成datastore,像这样:Golang GAE - HTML模板未正确插入链接到网页

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链接?

回答

5

"http/template"包会自动转义所有字符串。要解决此问题,您必须创建类型为template.HTML的值。例如。

import "html/template" 

type Foo struct { 
    Bar template.HTML 
} 

然后在你的代码做这样的事情:

Foo.Bar = template.HTML(barString) 
+0

对于第二部分 - '美孚Foo.Bar'的'[ “酒吧”]'? – ThePiachu

+0

@ThePiachu是的,它应该是'Foo.Bar' –