您的位置 首页 golang

Golang Web从入门到精通 2张图搞定Gin多模板继承

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}}  

文章来源:智云一二三科技

文章标题:Golang Web从入门到精通 2张图搞定Gin多模板继承

文章地址:https://www.zhihuclub.com/86876.shtml

关于作者: 智云科技

热门文章

发表评论

您的电子邮箱地址不会被公开。

网站地图