From 9b9adb5d0b353c0866aac9582d673afc7e93accb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E8=B6=85?= <17805310388@139.com> Date: Wed, 20 Dec 2023 16:50:05 +0800 Subject: [PATCH] templates test --- go.mod | 32 ++++++++++++++++++++++++++++++++ main.go | 17 +++++++++++++++++ templates/index.tmpl | 15 +++++++++++++++ templates/page1.tmpl | 12 ++++++++++++ templates/page2 copy.tmpl | 12 ++++++++++++ templates/page2.tmpl | 12 ++++++++++++ templates/page3.tmpl | 12 ++++++++++++ 7 files changed, 112 insertions(+) create mode 100644 go.mod create mode 100644 main.go create mode 100644 templates/index.tmpl create mode 100644 templates/page1.tmpl create mode 100644 templates/page2 copy.tmpl create mode 100644 templates/page2.tmpl create mode 100644 templates/page3.tmpl diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ff4d4eb --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/main.go b/main.go new file mode 100644 index 0000000..8913ca4 --- /dev/null +++ b/main.go @@ -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") +} diff --git a/templates/index.tmpl b/templates/index.tmpl new file mode 100644 index 0000000..7a26886 --- /dev/null +++ b/templates/index.tmpl @@ -0,0 +1,15 @@ + + +
+This is a simple blog written using Go and Gin framework.
+ Go语言入门This is the content of page 1.
+ + diff --git a/templates/page2 copy.tmpl b/templates/page2 copy.tmpl new file mode 100644 index 0000000..133ccfe --- /dev/null +++ b/templates/page2 copy.tmpl @@ -0,0 +1,12 @@ + + + +This is the content of page 2.这个是page2的copy版本
+ + diff --git a/templates/page2.tmpl b/templates/page2.tmpl new file mode 100644 index 0000000..d01aade --- /dev/null +++ b/templates/page2.tmpl @@ -0,0 +1,12 @@ + + + +This is the content of page 2.再加几个汉字
+ + diff --git a/templates/page3.tmpl b/templates/page3.tmpl new file mode 100644 index 0000000..0d95e39 --- /dev/null +++ b/templates/page3.tmpl @@ -0,0 +1,12 @@ + + + +This is the content of page 3.
+ +