Golang render string as html in Revel

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

Golang render string as html in Revel

问题

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

import "html/template"

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

然后在视图中:

{{template "header.html" .}}

{{.bigDig}}

{{template "footer.html" .}}

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

英文:

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

func (c Pages) Show(url string) revel.Result {    
	bigDig := &quot;&lt;h1&gt;Hello, dig!&lt;/h1&gt;&quot;

	return c.Render(bigDig)
}

and then in view:

{{template &quot;header.html&quot; .}}

{{.bigDig}}

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

确定