Golang render string as html in Revel

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

Golang render string as html in Revel

问题

Revel中,你可以使用template.HTML类型来将字符串渲染为HTML。以下是一个示例代码:

  1. import "html/template"
  2. func (c Pages) Show(url string) revel.Result {
  3. bigDig := template.HTML("<h1>Hello, dig!</h1>")
  4. return c.Render(bigDig)
  5. }

然后在视图中:

  1. {{template "header.html" .}}
  2. {{.bigDig}}
  3. {{template "footer.html" .}}

这样就可以将字符串作为HTML进行渲染了。希望对你有帮助!

英文:

I need to render string as html in Revel. How to do this?
I've tried:

  1. func (c Pages) Show(url string) revel.Result {
  2. bigDig := &quot;&lt;h1&gt;Hello, dig!&lt;/h1&gt;&quot;
  3. return c.Render(bigDig)
  4. }

and then in view:

  1. {{template &quot;header.html&quot; .}}
  2. {{.bigDig}}
  3. {{template &quot;footer.html&quot; .}}

but it doesn't work. How to do this?

答案1

得分: 2

你的变量需要是 template.HTML 类型。

请阅读 http://golang.org/pkg/html/template/#HTMLhttps://groups.google.com/forum/#!topic/golang-nuts/8L4eDkr5Q84

英文:

Your var need to be in a template.HTML type.

Read http://golang.org/pkg/html/template/#HTML or https://groups.google.com/forum/#!topic/golang-nuts/8L4eDkr5Q84.

答案2

得分: 2

你应该将你的 {{.bigDig}} 改为 {{raw .bigDig}}

英文:

You should change your {{.bigDig}} into {{raw .bigDig}}

huangapple
  • 本文由 发表于 2014年7月11日 15:17:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/24692092.html
匿名

发表评论

匿名网友

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

确定