From 908cf3b6ed8ed43bcccbfa8f190b711f5a30fbab Mon Sep 17 00:00:00 2001 From: nannanwu Date: Mon, 24 Nov 2025 16:17:22 +0800 Subject: [PATCH] =?UTF-8?q?gin-HTML=E6=A8=A1=E6=9D=BF=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 48 +++++++++++++++----------- templates/pages/about.html | 9 ----- templates/pages/articles.html | 13 -------- templates/pages/contact.html | 14 -------- templates/pages/index.html | 63 ++++++++++++++++++++++++++++++----- 5 files changed, 83 insertions(+), 64 deletions(-) delete mode 100644 templates/pages/about.html delete mode 100644 templates/pages/articles.html delete mode 100644 templates/pages/contact.html diff --git a/main.go b/main.go index 78f0e45..e23bf07 100644 --- a/main.go +++ b/main.go @@ -4,36 +4,44 @@ import ( "github.com/gin-gonic/gin" ) +type Article struct { + Title string + Content string +} + func main() { r := gin.Default() - - // 静态资源映射 - r.Static("/static", "./static") - // 加载模板(支持多层目录) r.LoadHTMLGlob("templates/**/*") + r.Static("/static", "./static") // 路由 r.GET("/", func(c *gin.Context) { - c.HTML(200, "index", gin.H{}) - }) + c.HTML(200, "pages/index.html", gin.H{ + "title": "Main website", + "score": 60, + "hobby": []string{"吃饭", "睡觉", "打豆豆"}, + "newsList": []interface{}{ + Article{ + Title: "新闻标题", + Content: "新闻内容1", + }, + Article{ + Title: "新闻标题2", + Content: "新闻内容2", + }, + }, + "newsList2": []string{}, - r.GET("/about", func(c *gin.Context) { - c.HTML(200, "about", gin.H{}) - }) - - r.GET("/articles", func(c *gin.Context) { - c.HTML(200, "articles", gin.H{ - "Articles": []map[string]string{ - {"Title": "Gin框架入门", "Date": "2025-11-23"}, - {"Title": "Go语言最佳实践", "Date": "2025-11-20"}, + "news": Article{ + Title: "标题", + Content: "测试2", }, }) }) - r.GET("/contact", func(c *gin.Context) { - c.HTML(200, "contact", gin.H{}) - }) - - r.Run(":8080") + err := r.Run(":8080") + if err != nil { + return + } } diff --git a/templates/pages/about.html b/templates/pages/about.html deleted file mode 100644 index 4c14f26..0000000 --- a/templates/pages/about.html +++ /dev/null @@ -1,9 +0,0 @@ -{{ define "about" }} - {{ template "base" . }} - - {{ block "title" . }}关于我们{{ end }} - {{ block "content" . }} -

关于我们

-

这里可以写团队介绍、网站背景或使命。

- {{ end }} -{{ end }} diff --git a/templates/pages/articles.html b/templates/pages/articles.html deleted file mode 100644 index 5963a51..0000000 --- a/templates/pages/articles.html +++ /dev/null @@ -1,13 +0,0 @@ -{{ define "articles" }} - {{ template "base" . }} - - {{ block "title" . }}文章列表{{ end }} - {{ block "content" . }} -

文章列表

- - {{ end }} -{{ end }} diff --git a/templates/pages/contact.html b/templates/pages/contact.html deleted file mode 100644 index c94c582..0000000 --- a/templates/pages/contact.html +++ /dev/null @@ -1,14 +0,0 @@ -{{ define "contact" }} - {{ template "base" . }} - - {{ block "title" . }}联系我们{{ end }} - {{ block "content" . }} -

联系我们

-
-
-
-
- -
- {{ end }} -{{ end }} diff --git a/templates/pages/index.html b/templates/pages/index.html index 1eb2c1b..771241d 100644 --- a/templates/pages/index.html +++ b/templates/pages/index.html @@ -1,9 +1,56 @@ -{{ define "index" }} - {{ template "base" . }} +{{define "pages/index.html"}} + + + + + + {{.title}} + + + {{/*输出*/}} +

{{.title}}

- {{ block "title" . }}首页{{ end }} - {{ block "content" . }} -

欢迎来到我的 Gin 网站

-

这里是首页内容,可以展示最新文章或公告。

- {{ end }} -{{ end }} + {{/*定义变量*/}} + {{$t :=.title}} +

{{$t}}

+ + + {{/*条件判断*/}} + {{if ge .score 60}} +

及格

+ {{else}} +

+ {{end}} + + {{/*循环遍历数据*/}} + + {{range $key,$value:=.hobby}} + + {{end}} + + {{range $key,$value:=.newsList}} + + {{end}} + + {{range $key,$value:=.newsList2}} + + {{else}} +
  • 没有数据
  • + {{end}} + + {{/*结构结构*/}} + {{with .news}} + {{.Title}} + {{.Content}} + {{end}} + + + + +{{end}} \ No newline at end of file