templates test

This commit is contained in:
张超
2023-12-20 16:50:05 +08:00
parent 4129bf5d3f
commit 9b9adb5d0b
7 changed files with 112 additions and 0 deletions

32
go.mod Normal file
View File

@@ -0,0 +1,32 @@
module go_blog
go 1.20
require github.com/gin-gonic/gin v1.9.1
require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

17
main.go Normal file
View File

@@ -0,0 +1,17 @@
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.GET("/", func(c *gin.Context) {
c.HTML(200, "index.tmpl", nil)
})
r.GET("/page/:id", func(c *gin.Context) {
id := c.Param("id")
c.HTML(200, "page"+id+".tmpl", nil)
})
r.Run(":8080")
}

15
templates/index.tmpl Normal file
View File

@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>My Blog</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Welcome to my blog!</h1>
<p>This is a simple blog written using Go and Gin framework.</p>
<a href="/page/1">Go语言入门</a><br>
<a href="/page/2">Gin框架入门</a><br>
<a href="/page/3">Web开发基础</a><br>
</body>
</html>

12
templates/page1.tmpl Normal file
View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Page 1</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Page 1</h1>
<p>This is the content of page 1.</p>
</body>
</html>

12
templates/page2 copy.tmpl Normal file
View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Page 2copy</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Page 2</h1>
<p>This is the content of page 2.这个是page2的copy版本</p>
</body>
</html>

12
templates/page2.tmpl Normal file
View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Page 2</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Page 2</h1>
<p>This is the content of page 2.再加几个汉字</p>
</body>
</html>

12
templates/page3.tmpl Normal file
View File

@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>Page 3</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Page 3</h1>
<p>This is the content of page 3.</p>
</body>
</html>