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 }} + {{/*定义变量*/}} + {{$t :=.title}} +及格
+ {{else}} + + {{end}} + + {{/*循环遍历数据*/}} + + {{range $key,$value:=.hobby}} +