8 个答案:
答案 0 :(得分:47)
将您的[]byte或string转换为template.HTML类型(已记录为here)
p.Body = template.HTML(s) // where s is a string or []byte
然后,在您的模板中,只需:
{{.Body}}
它将被打印而不会逃脱。
修改强>
为了能够在您网页的正文中包含HTML,您需要更改Page类型声明:
type Page struct {
Title string
Body template.HTML
}
然后分配给它。
答案 1 :(得分:16)
查看template.HTML类型。它可以用于封装HTML的已知安全片段(如Markdown的输出)。 “html / template”包不会逃避此类型。
type Page struct {
Title string
Body template.HTML
}
page := &Page{
Title: "Example",
Body: template.HTML(blackfriday.MarkdownCommon([]byte("foo bar")),
}
我通常编写自己的func Markdown(text string) html.Template方法,使用适当的配置调用blackfriday并执行某些类型的转换。另一种替代方法可能是在模板解析器中注册一个“html”函数,它允许您通过执行{{html .MySafeStr}}之类的操作来输出任何值而不进行任何转义。代码可能如下所示:
var tmpl = template.Must(template.New("").Funcs(template.FuncMap{
"html": func(value interface{}) template.HTML {
return template.HTML(fmt.Sprint(value))
},
}).ParseFiles("file1.html", "file2.html"))
答案 2 :(得分:7)
我为模板创建了一个自定义函数,如下所示:
func noescape(str string) template.HTML {
return template.HTML(str)
}
var fn = template.FuncMap{
"noescape": noescape,
}
然后在你的模板上:
{{ noescape $x.Body }}
答案 3 :(得分:1)
这是一种不需要对现有结构进行任何更改的方法,也是对模板进行非常小的附加更改的方法:
更改这些行:
composer require illuminate/notifications:~5.3
到此(包含一个带有将输出未转义HTML的函数的funcmap):
var (
templates = template.Must(template.ParseFiles("tmpl/edit.html", "tmpl/view.html"))
)
然后只需更改模板HTML:
var templates = template.Must(template.New("main").Funcs(template.FuncMap{
"safeHTML": func(b []byte) template.HTML {
return template.HTML(b)
},
}).ParseFiles("tmpl/edit.html", "tmpl/view.html"))
到此(使用你的新功能):
更容易!
答案 4 :(得分:1)
我正在使用Beego和React.js,并试图让JSX解析器运行数小时。结果是html / template剥离注释特别是js doc block / ** @jsx React.DOM * /。
通过创建一个特殊方法来解决它,将注释键入为JS并从模板中调用它。
// Create a method in your controller (I'm using Beego)
func jsxdoc()(out template.JS) {
return template.JS(`/** @jsx React.DOM */`)
}
// Add method to your function map available to views
beego.AddFuncMap("jsxdoc", jsxdoc)
// In template
{{ jsxdoc }}
var CommentBox = React.createClass({
render: function() {
return (
);
}
});
React.renderComponent(
,
document.getElementById('content')
);
答案 5 :(得分:0)
有关澄清以及将HTML传递给模板的更简单方法,请参阅
只需通过go创建HTML字符串并将其传递到模板中,例如:
Sout := ""
.
.
Sout += fmt.Sprintf(`
%s%.2f%s%s%s%s%d%d%d`, AccountID, amount, remissiondetails, created, begins, ends,freePDFs, freeinformants, freeSDQs)
.
.
render(w, "templates/Waivers.html", map[string]interface{}{ "Body":template.HTML(Sout), })
答案 6 :(得分:0)
在我的情况下(我在视图struct中填充了Activity的列表),我不得不将属性Message string更改为Message template.HTML。然后,在设置属性值时,我可以调用activity.Message = template.HTML("The HTML")。
答案 7 :(得分:-1)
为什么不将[]byte转换为字符串?你可以这样做:
str := string(page.Body)
Hello, world! I am a CommentBox.