main.go源码:
package main
import (
"github.com/gin-contrib/multitemplate"
"github.com/gin-gonic/gin"
"net/http"
)
func render() multitemplate.Renderer {
r := multitemplate.NewRenderer()
r.AddFromFiles(
"page_index",
"view/base.html",
"view/header.html",
"view/footer.html",
"view/index.html",
)
return r
}
func main() {
app := gin.Default()
app.HTMLRender = render()
app.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "page_index", nil)
})
_ = app.Run("127.0.0.1:80")
}
base.html源码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>base</h1>
{{block "head" .}}
{{template "header.html" .}}
{{end}}
{{block "body" .}}body{{end}}
{{block "foot" .}}
{{template "footer.html" .}}
{{end}}
</body>
</html>
header.html源码
<h3 style="background-color: red">
header.html
</h3>
footer.html源码
<h3 style="background-color: red">
header.html
</h3>
index.html源码
{{template "base.html" .}}
{{define "body"}}
<h1>index.html页面</h1>
{{end}}
{{define "foot" }}
{{template "footer.html" .}}
{{end}}
本文暂时没有评论,来添加一个吧(●'◡'●)