在Go中的HTML模板化 – 将HTML作为管道的值插入

huangapple go评论78阅读模式
英文:

HTML Templating in Go - insert html as value of pipleline

问题

<html>
<head>
</head>

&lt;body&gt;
	{{range .ItemList}}
	&lt;div class=&quot;news-item&quot;&gt;
		&lt;p&gt;
			&lt;a href=&quot;{{.Link}}&quot;&gt;{{.Title}}&lt;/a&gt;
		&lt;/p&gt;
		&lt;p&gt;{{.Description}}&lt;/p&gt;
	&lt;/div&gt;
	{{end}}
&lt;/body&gt;

</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 (
	&quot;fmt&quot;
	&quot;io/ioutil&quot;
	&quot;log&quot;
	&quot;net/http&quot;
	&quot;html/template&quot;
	&quot;encoding/xml&quot;
)

type RSS struct {
	XMLName xml.Name `xml:&quot;rss&quot;`
	Items Items `xml:&quot;channel&quot;`
}
type Items struct {
	XMLName xml.Name `xml:&quot;channel&quot;`
	ItemList []Item `xml:&quot;item&quot;`
}
type Item struct {
	Title string `xml:&quot;title&quot;`
	Link string `xml:&quot;link&quot;`
	Description string `xml:&quot;description&quot;`
}

func main() {
	res, err := http.Get(&quot;http://news.google.com/news?hl=en&amp;gl=us&amp;q=samsung&amp;um=1&amp;ie=UTF-8&amp;output=rss&quot;)
	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), &amp;i)
	if err != nil {
        log.Fatal(err)  
    }
	
	res.Body.Close()
	
	http.HandleFunc(&quot;/&quot;, func(w http.ResponseWriter, r *http.Request) {
              handler(w, r, i)
       })
    http.ListenAndServe(&quot;:8080&quot;, nil)
}

func handler(w http.ResponseWriter, r *http.Request, i RSS) {
	t, _ := template.ParseFiles(&quot;index.html&quot;)
    t.Execute(w, i.Items)
}

this is the html:

&lt;html&gt;
	&lt;head&gt;
	&lt;/head&gt;
	
	&lt;body&gt;
		{{range .ItemList}}
		&lt;div class=&quot;news-item&quot;&gt;
			&lt;p&gt;
				&lt;a href=&quot;{{.Link}}&quot;&gt;{{.Title}}&lt;/a&gt;
			&lt;/p&gt;
			&lt;p&gt;{{.Description}}&lt;/p&gt;
		&lt;/div&gt;
		{{end}}
	&lt;/body&gt;
&lt;/html&gt;

and the output looks like this:

&lt;div class=&quot;news-item&quot;&gt;
			&lt;p&gt;
				&lt;a href=&quot;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/&quot;&gt;What Apple Might Learn from Samsung - Private Equity Hub (press release)&lt;/a&gt;
			&lt;/p&gt;
			&lt;p&gt;&amp;lt;table border=&amp;#34;0&amp;#34; cellpadding=&amp;#34;2&amp;#34; cellspacing=&amp;#34;7&amp;#34; style=&amp;#34;vertical-align:top;&amp;#34;&amp;gt;&amp;lt;tr&amp;gt;&amp;lt;td width=&amp;#34;80&amp;#34; align=&amp;#34;center&amp;#34; valign=&amp;#34;top&amp;#34;&amp;gt;&amp;lt;font style=&amp;#34;font-size:85%;font-family:arial,sans-serif&amp;#34;&amp;gt;&amp;lt;/font&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;td valign=&amp;#34;top&amp;#34; class=&amp;#34;j&amp;#34;&amp;gt;&amp;lt;font style=&amp;#34;font-size:85%;font-family:arial,sans-serif&amp;#34;&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;div style=&amp;#34;padding-top:0.8em;&amp;#34;&amp;gt;&amp;lt;img alt=&amp;#34;&amp;#34; height=&amp;#34;1&amp;#34; width=&amp;#34;1&amp;#34; /&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div class=&amp;#34;lh&amp;#34;&amp;gt;&amp;lt;a href=&amp;#34;http://news.google.com/news/url?sa=t&amp;amp;amp;fd=R&amp;amp;amp;usg=AFQjCNFd-5CF7Rwy7sjNZ2-fSOLkO6ri5g&amp;amp;amp;url=http://www.pehub.com/186539/what-apple-might-learn-samsung/&amp;#34;&amp;gt;&amp;lt;b&amp;gt;What Apple Might Learn from &amp;lt;b&amp;gt;Samsung&amp;lt;/b&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;font size=&amp;#34;-1&amp;#34;&amp;gt;&amp;lt;b&amp;gt;&amp;lt;font color=&amp;#34;#6f6f6f&amp;#34;&amp;gt;Private Equity Hub (press release)&amp;lt;/font&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;/font&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;font size=&amp;#34;-1&amp;#34;&amp;gt;&amp;lt;b&amp;gt;Samsung&amp;lt;/b&amp;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. &amp;lt;b&amp;gt;Samsung&amp;amp;#39;s&amp;lt;/b&amp;gt; handsets are hot. Late last year, &amp;lt;b&amp;gt;Samsung&amp;amp;#39;s&amp;lt;/b&amp;gt; Galaxy S III became the best-selling smartphone in &amp;lt;b&amp;gt;...&amp;lt;/b&amp;gt;&amp;lt;/font&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;font size=&amp;#34;-1&amp;#34; class=&amp;#34;p&amp;#34;&amp;gt;&amp;lt;/font&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;font class=&amp;#34;p&amp;#34; size=&amp;#34;-1&amp;#34;&amp;gt;&amp;lt;a class=&amp;#34;p&amp;#34; href=&amp;#34;http://news.google.com/news/more?ncl=d2dovyDH3OFX_MM&amp;amp;amp;ned=us&amp;#34;&amp;gt;&amp;lt;nobr&amp;gt;&amp;lt;b&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;/nobr&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/font&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/font&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&amp;lt;/table&amp;gt;&lt;/p&gt;
		&lt;/div&gt;

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 := &amp;pipeObject{
    template.HTML(&quot;&lt;p&gt;Your safe HTML&lt;/p&gt;&quot;),
}

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{      
    &quot;safe&quot;: func(s string) template.HTML {
        return template.HTML(s)
     },
}

template.Must(template.New(&quot;Template&quot;).Funcs(funcMap).ParseFiles(files...))

Use it in the template file:

 {{.Description|safe}}

huangapple
  • 本文由 发表于 2013年2月14日 20:00:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/14874384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定