1 回答

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個贊
您可以使用define定義部分和template混合多個 HTML 部分。
package main
import (
"html/template"
"github.com/gin-gonic/gin"
)
var (
partial1 = `{{define "elm1"}}<div>element1</div>{{end}}`
partial2 = `{{define "elm2"}}<div>element2</div>{{end}}`
body = `{{template "elm1"}}{{template "elm2"}}`
)
func main() {
// Or use `ParseFiles` to parse tmpl files instead
t := template.Must(template.New("elements").Parse(body))
app := gin.Default()
app.GET("/", func(c *gin.Context) {
c.HTML(200, "elements", nil)
})
app.Run(":8000")
}
這是閱讀https://gohugo.io/templates/go-templates/的好地方
- 1 回答
- 0 關(guān)注
- 243 瀏覽
添加回答
舉報(bào)