douji6940 2013-02-14 12:00
浏览 20
已采纳

Go中的HTML模板-插入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

  • 写回答

2条回答 默认 最新

  • drgovyk64676 2013-02-14 18:35
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作