我正在为我的网络应用程序使用 Go gin gonic。如何在 1 页中多次使用相同的模板文件,并将不同的变量传递给模板。
段.tmpl
{{ define "segment" }}
<div>{{ .Variable }}</div>
{{ end }}
布局.tmpl
<!DOCTYPE HTML>
<html>
<body>
{{ template "segment . }} #with a variable 1
{{ template "segment . }} #with different variable
{{ template "segment . }} #another same template with another
</body>
</html>
main.go
r.GET("/home/", func(c *gin.Context) {
tmpl := template.Must(template.ParseFiles("templates/layout.tmpl", "templates/product_add.tmpl", "templates/segment.tmpl")
r.SetHTMLTemplate(tmpl)
c.HTML(200, "layout", gin.H {
"Variable1": "var1",
"variable2": "var2",
})
}
如何在“主页”页面中多次使用 segment.tmpl 并将不同类型的变量传递给 segment.tmpl?我到处搜索,一无所获,最接近的是template.Clone,但仍然找不到它的任何例子。
呼如林
凤凰求蛊
随时随地看视频慕课网APP
相关分类