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

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?