英文:
HTML Templating in Go - insert html as value of pipleline
问题
<html>
<head>
</head>
<body>
{{range .ItemList}}
<div class="news-item">
<p>
<a href="{{.Link}}">{{.Title}}</a>
</p>
<p>{{.Description}}</p>
</div>
{{end}}
</body>
</html>
英文:
I'm using a Go template to output html, and inserting some values through a pipeline. The thing is one of the values a raw html that I don't want to be escaped. But when the template is executed, it is escaped.
This is the code
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"html/template"
"encoding/xml"
)
type RSS struct {
XMLName xml.Name `xml:"rss"`
Items Items `xml:"channel"`
}
type Items struct {
XMLName xml.Name `xml:"channel"`
ItemList []Item `xml:"item"`
}
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
}
func main() {
res, err := http.Get("http://news.google.com/news?hl=en&gl=us&q=samsung&um=1&ie=UTF-8&output=rss")
if err != nil {
log.Fatal(err)
}
asText, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
var i RSS
err = xml.Unmarshal([]byte(asText), &i)
if err != nil {
log.Fatal(err)
}
res.Body.Close()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
handler(w, r, i)
})
http.ListenAndServe(":8080", nil)
}
func handler(w http.ResponseWriter, r *http.Request, i RSS) {
t, _ := template.ParseFiles("index.html")
t.Execute(w, i.Items)
}
this is the html:
<html>
<head>
</head>
<body>
{{range .ItemList}}
<div class="news-item">
<p>
<a href="{{.Link}}">{{.Title}}</a>
</p>
<p>{{.Description}}</p>
</div>
{{end}}
</body>
</html>
and the output looks like this:
<div class="news-item">
<p>
<a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;usg=AFQjCNFd-5CF7Rwy7sjNZ2-fSOLkO6ri5g&amp;url=http://www.pehub.com/186539/what-apple-might-learn-samsung/">What Apple Might Learn from Samsung - Private Equity Hub (press release)</a>
</p>
<p>&lt;table border=&#34;0&#34; cellpadding=&#34;2&#34; cellspacing=&#34;7&#34; style=&#34;vertical-align:top;&#34;&gt;&lt;tr&gt;&lt;td width=&#34;80&#34; align=&#34;center&#34; valign=&#34;top&#34;&gt;&lt;font style=&#34;font-size:85%;font-family:arial,sans-serif&#34;&gt;&lt;/font&gt;&lt;/td&gt;&lt;td valign=&#34;top&#34; class=&#34;j&#34;&gt;&lt;font style=&#34;font-size:85%;font-family:arial,sans-serif&#34;&gt;&lt;br /&gt;&lt;div style=&#34;padding-top:0.8em;&#34;&gt;&lt;img alt=&#34;&#34; height=&#34;1&#34; width=&#34;1&#34; /&gt;&lt;/div&gt;&lt;div class=&#34;lh&#34;&gt;&lt;a href=&#34;http://news.google.com/news/url?sa=t&amp;amp;fd=R&amp;amp;usg=AFQjCNFd-5CF7Rwy7sjNZ2-fSOLkO6ri5g&amp;amp;url=http://www.pehub.com/186539/what-apple-might-learn-samsung/&#34;&gt;&lt;b&gt;What Apple Might Learn from &lt;b&gt;Samsung&lt;/b&gt;&lt;/b&gt;&lt;/a&gt;&lt;br /&gt;&lt;font size=&#34;-1&#34;&gt;&lt;b&gt;&lt;font color=&#34;#6f6f6f&#34;&gt;Private Equity Hub (press release)&lt;/font&gt;&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&lt;font size=&#34;-1&#34;&gt;&lt;b&gt;Samsung&lt;/b&gt; suddenly seems a lot like a boxer whose every punch at the world champion, Apple, is bringing it closer to a legitimate shot at the title. &lt;b&gt;Samsung&amp;#39;s&lt;/b&gt; handsets are hot. Late last year, &lt;b&gt;Samsung&amp;#39;s&lt;/b&gt; Galaxy S III became the best-selling smartphone in &lt;b&gt;...&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&lt;font size=&#34;-1&#34; class=&#34;p&#34;&gt;&lt;/font&gt;&lt;br /&gt;&lt;font class=&#34;p&#34; size=&#34;-1&#34;&gt;&lt;a class=&#34;p&#34; href=&#34;http://news.google.com/news/more?ncl=d2dovyDH3OFX_MM&amp;amp;ned=us&#34;&gt;&lt;nobr&gt;&lt;b&gt;&lt;/b&gt;&lt;/nobr&gt;&lt;/a&gt;&lt;/font&gt;&lt;/div&gt;&lt;/font&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</p>
</div>
the description is escaped html, and i want it regular html
答案1
得分: 10
将你的管道的描述字段的类型从string
改为template.HTML
,如下所示:
type pipeObject struct {
Description template.HTML
}
pipe := &pipeObject{
template.HTML("<p>Your safe HTML</p>"),
}
相关文档:template.HTML
英文:
Make your pipeline's description field of type template.HTML
instead of string
, like so:
type pipeObject struct {
Description template.HTML
}
pipe := &pipeObject{
template.HTML("<p>Your safe HTML</p>"),
}
Relevant documentation: template.HTML
答案2
得分: 7
将字符串转换为template.HTML并将安全函数添加到funcMap中
funcMap := template.FuncMap{
"safe": func(s string) template.HTML {
return template.HTML(s)
},
}
template.Must(template.New("Template").Funcs(funcMap).ParseFiles(files...))
在模板文件中使用它:
{{.Description|safe}}
英文:
add safe function to funcMap which can convert string to template.HTML
funcMap := template.FuncMap{
"safe": func(s string) template.HTML {
return template.HTML(s)
},
}
template.Must(template.New("Template").Funcs(funcMap).ParseFiles(files...))
Use it in the template file:
{{.Description|safe}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论