W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
我們首先創(chuàng)建一個(gè)存放模板文件的templates文件夾,然后在其內(nèi)部寫(xiě)入一個(gè)index.html,代碼如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>
賬號(hào)是:{{.name}}
</h1>
<h1>
密碼是:{{.pwd}}
</h1>
</body>
</html>
在Gin框架中使用?LoadHTMLGlob()
? 或者 ?LoadHTMLFiles()
?來(lái)渲染HTML模板
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.LoadHTMLGlob("./templates/*")
r.GET("/demo",func(c *gin.Context) {
c.HTML(http.StatusOK,"index.html",gin.H{
"name":"admin",
"pwd":"123456",
})
})
r.Run()
}
運(yùn)行上述代碼,并在瀏覽器中訪問(wèn)127.0.0.1:8080/demo
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話(huà):173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: