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

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

HTML Templating in Go - insert html as value of pipleline

问题

<html>
<head>
</head>

  1. &lt;body&gt;
  2. {{range .ItemList}}
  3. &lt;div class=&quot;news-item&quot;&gt;
  4. &lt;p&gt;
  5. &lt;a href=&quot;{{.Link}}&quot;&gt;{{.Title}}&lt;/a&gt;
  6. &lt;/p&gt;
  7. &lt;p&gt;{{.Description}}&lt;/p&gt;
  8. &lt;/div&gt;
  9. {{end}}
  10. &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

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;io/ioutil&quot;
  5. &quot;log&quot;
  6. &quot;net/http&quot;
  7. &quot;html/template&quot;
  8. &quot;encoding/xml&quot;
  9. )
  10. type RSS struct {
  11. XMLName xml.Name `xml:&quot;rss&quot;`
  12. Items Items `xml:&quot;channel&quot;`
  13. }
  14. type Items struct {
  15. XMLName xml.Name `xml:&quot;channel&quot;`
  16. ItemList []Item `xml:&quot;item&quot;`
  17. }
  18. type Item struct {
  19. Title string `xml:&quot;title&quot;`
  20. Link string `xml:&quot;link&quot;`
  21. Description string `xml:&quot;description&quot;`
  22. }
  23. func main() {
  24. 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;)
  25. if err != nil {
  26. log.Fatal(err)
  27. }
  28. asText, err := ioutil.ReadAll(res.Body)
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. var i RSS
  33. err = xml.Unmarshal([]byte(asText), &amp;i)
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. res.Body.Close()
  38. http.HandleFunc(&quot;/&quot;, func(w http.ResponseWriter, r *http.Request) {
  39. handler(w, r, i)
  40. })
  41. http.ListenAndServe(&quot;:8080&quot;, nil)
  42. }
  43. func handler(w http.ResponseWriter, r *http.Request, i RSS) {
  44. t, _ := template.ParseFiles(&quot;index.html&quot;)
  45. t.Execute(w, i.Items)
  46. }

this is the html:

  1. &lt;html&gt;
  2. &lt;head&gt;
  3. &lt;/head&gt;
  4. &lt;body&gt;
  5. {{range .ItemList}}
  6. &lt;div class=&quot;news-item&quot;&gt;
  7. &lt;p&gt;
  8. &lt;a href=&quot;{{.Link}}&quot;&gt;{{.Title}}&lt;/a&gt;
  9. &lt;/p&gt;
  10. &lt;p&gt;{{.Description}}&lt;/p&gt;
  11. &lt;/div&gt;
  12. {{end}}
  13. &lt;/body&gt;
  14. &lt;/html&gt;

and the output looks like this:

  1. &lt;div class=&quot;news-item&quot;&gt;
  2. &lt;p&gt;
  3. &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;
  4. &lt;/p&gt;
  5. &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;
  6. &lt;/div&gt;

the description is escaped html, and i want it regular html

答案1

得分: 10

将你的管道的描述字段的类型从string改为template.HTML,如下所示:

  1. type pipeObject struct {
  2. Description template.HTML
  3. }
  4. pipe := &pipeObject{
  5. template.HTML("<p>Your safe HTML</p>"),
  6. }

相关文档:template.HTML

英文:

Make your pipeline's description field of type template.HTML instead of string, like so:

  1. type pipeObject struct {
  2. Description template.HTML
  3. }
  4. pipe := &amp;pipeObject{
  5. template.HTML(&quot;&lt;p&gt;Your safe HTML&lt;/p&gt;&quot;),
  6. }

Relevant documentation: template.HTML

答案2

得分: 7

将字符串转换为template.HTML并将安全函数添加到funcMap中

  1. funcMap := template.FuncMap{
  2. "safe": func(s string) template.HTML {
  3. return template.HTML(s)
  4. },
  5. }
  6. template.Must(template.New("Template").Funcs(funcMap).ParseFiles(files...))

在模板文件中使用它:

  1. {{.Description|safe}}
英文:

add safe function to funcMap which can convert string to template.HTML

  1. funcMap := template.FuncMap{
  2. &quot;safe&quot;: func(s string) template.HTML {
  3. return template.HTML(s)
  4. },
  5. }
  6. template.Must(template.New(&quot;Template&quot;).Funcs(funcMap).ParseFiles(files...))

Use it in the template file:

  1. {{.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:

确定