2

我正在使用 Go 模板输出 html,并通过管道插入一些值。事情是我不想被转义的原始 html 的值之一。但是当模板被执行时,它被转义了。

这是代码

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)
}

这是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>

输出如下所示:

<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>

描述是转义的 html,我想要它是常规的 html

4

2 回答 2

10

将管道的描述字段设置为 typetemplate.HTML而不是string,如下所示:

type pipeObject struct {
    Description template.HTML
} 

pipe := &pipeObject{
    template.HTML("<p>Your safe HTML</p>"),
}

相关文档:template.HTML

于 2013-02-14T18:35:19.213 回答
7

为funcMap添加安全函数,可以将字符串转换为template.HTML

funcMap := template.FuncMap{      
    "safe": func(s string) template.HTML {
        return template.HTML(s)
     },
}

template.Must(template.New("Template").Funcs(funcMap).ParseFiles(files...))

在模板文件中使用它:

 {{.Description|safe}}
于 2013-02-14T12:13:01.570 回答