Gin路由中相应数据 c.HTML()
{{define "default/index.html"}}
{{end}}
- news.html
{{define "default/news.html"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h2>{{.title}}</h2>
<p>
{{.news.Title}}
</p>
<p>
{{.news.Content}}
</p>
</body>
</html>
{{end}}
- main.go
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
type Aritle struct {
Title string
Content string
}
func main() {
r := gin.Default()
r.LoadHTMLGlob("templates/**/*")
// r.GET("/123", func(c *gin.Context) {
// c.String(200, "你好gin")
// })
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "default/news.html", gin.H{
"title": "首页",
})
})
news := Aritle{
Title: "文章标题",
Content: "文章内容",
}
r.GET("/news", func(c *gin.Context) {
c.HTML(200, "default/news.html", gin.H{
"title": "新闻",
"news": news,
})
})
r.Run()
}
评论
发表评论