其他分享
首页 > 其他分享> > 【Gin框架】Gin模板解析与渲染

【Gin框架】Gin模板解析与渲染

作者:互联网

main.go

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()
	// 模板解析
	r.LoadHTMLFiles("templates/index.tmpl")
	r.GET("/", func(c *gin.Context){
		// 模板渲染
		c.HTML(http.StatusOK, "index.tmpl", gin.H{
			"title": "Hello Gin",
		})
	})
	r.Run(":9000")
}

templates/index.tmpl

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Gin</title>
</head>
<body>
{{.title}}
</body>
</html>

运行后访问:http://localhost:9000/ 会出现Hello Gin

标签:index,http,渲染,gin,Gin,tmpl,main,模板
来源: https://blog.csdn.net/qq2942713658/article/details/112689457