渲染

PPG007 ... 2021-12-26 Less than 1 minute

# 渲染

# 各种数据格式的响应

func main() {
  r := gin.Default()
  r.GET("/json", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{"message": "json", "status": "ok"})
  })
  r.GET("/struct", func(c *gin.Context) {
    user := User{
      Username: "PPG007",
      Password: "123456",
    }
    c.JSON(http.StatusOK, user)
  })
  r.GET("/xml", func(c *gin.Context) {
    c.XML(http.StatusOK, gin.H{"message": "json", "status": "ok"})
  })
  r.GET("/yaml", func(c *gin.Context) {
    c.YAML(http.StatusOK, gin.H{"message": "json", "status": "ok"})
  })
  r.Run(":8080")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# HTML 模板渲染

func main() {
  r := gin.Default()
  // 加载文件
  r.LoadHTMLGlob("*.html")
  r.GET("/", func(c *gin.Context) {
    // 指定要渲染的文件
    c.HTML(http.StatusOK, "index.html", gin.H{"username": "PPG007", "password": "123456"})
  })
  r.Run(":8080")
}
1
2
3
4
5
6
7
8
9
10

如果要使用不同路径下名称相同的模板:

func main() {
  router := gin.Default()
  // 设置自定义分隔符
  r.Delims("[[", "]]")
  router.LoadHTMLGlob("templates/**/*")
  router.GET("/posts/index", func(c *gin.Context) {
    c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{
      "title": "Posts",
    })
  })
  router.GET("/users/index", func(c *gin.Context) {
    c.HTML(http.StatusOK, "users/index.tmpl", gin.H{
      "title": "Users",
    })
  })
  router.Run(":8080")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

templates/posts/index.tmpl:

{{ define "posts/index.tmpl" }}
<html><h1>
  {{ .title }}
</h1>
<p>Using posts/index.tmpl</p>
</html>
{{ end }}
1
2
3
4
5
6
7

templates/posts/index/tmpl:

{{ define "users/index.tmpl" }}
<html><h1>
  {{ .title }}
</h1>
<p>Using users/index.tmpl</p>
</html>
{{ end }}
1
2
3
4
5
6
7
Last update: December 26, 2021 03:54
Contributors: PPG007