• gin 路由到模板(多应用模式--view为模板存放地址,admin为后台,web为前台)


    main.go文件

    package main
    
    import(
       "github.com/gin-gonic/gin"
       "net/http"
    )
    
    type Article struct {
       Title string
       Content string
    }
    
    func main()  {
       r := gin.Default()
       //r.LoadHTMLFiles()
       //多个框架是加/**/代表目录
       r.LoadHTMLGlob("view/**/*")
       //前端
       r.GET("/", func(c *gin.Context) {
          c.HTML(http.StatusOK,"web/index.html",gin.H{
             "title":"首页sss",
             "sort" : 55,
             "list" : []string{"我是谁","我是我","我是人","我是个好人你信吗?"},
             "newlist":[]interface{}{
               &Article{
                  Title:"新闻标题1",
                  Content:"新闻内容1",
               },
               &Article{
                  Title:"新闻标题2",
                  Content:"新闻内容2",
               },
            },
          })
    
       })
       r.GET("/news", func(c *gin.Context) {
          a:= &Article{
             Title:"新闻标题",
             Content:"新闻内容",
          }
          c.HTML(http.StatusOK,"web/news.html",gin.H{
             "title":"新闻页面",
             "news":a,
          })
    
       })
    
       //后端
       r.GET("/admin", func(c *gin.Context) {
          c.HTML(http.StatusOK,"admin/index.html",gin.H{
             "title":"首页sss",
          })
    
       })
       r.GET("/admin/news", func(c *gin.Context) {
          a:= &Article{
             Title:"新闻标题",
             Content:"新闻内容",
          }
          c.HTML(http.StatusOK,"admin/news.html",gin.H{
             "title":"新闻页面",
             "news":a,
          })
    
       })
    
       r.Run()
    }

    前端对应的模板文件:

    web/index.html

    
    {{ define "web/index.html" }}
    
    
    
    
        
        
        
        document
    
    
        

    {{.title}}

    {{$t := .title}}

    {{$t}}

    {{if ge .sort 60}}
    及格
    {{else}}
    不及格
    {{end}}          {{range $key,$value := .list}}     
  • key=>{{$key}}-----value=>{{$value}}
  •     {{end}}     
             {{range $key,$value := .newlist}}     
  • key=>{{$key}}-----value.Title=>{{$value.Title}}-------value.Content=>{{$value.Content}}
  •     {{else}}         
  • 切片中无数据
  •     {{end}}
    
    
    
    
    {{ end }}
    
    web/news.html
    
    
    {{ define "web/news.html" }}
    
    
    
        
        
        
        document
    
    
        
    WEB

    {{.title}}


    {{.news.Title}}
    {{.news.Content}}
    {{ end }}

    admin/index.html

    
    {{ define "admin/index.html" }}
    
    
    
        
        
        
        document
    
    
        

    {{.title}}

    {{ end }}
  • 相关阅读:
    【带头学C++】----- 九、类和对象 ---- 9.1 类和对象的基本概念----(9.1.4---9.1.6)
    Dapr与Service Mesh的区别
    MS9708数模转换器可pin对pin兼容AD9708
    请问各位程序员,是我的思维方式有错误吗?
    上周热点回顾(8.22-8.28)
    PMP每日一练 | 考试不迷路-11.28(包含敏捷+多选)
    软件测试概念总结
    ffmpeg和ffplay
    husky+lint-staged+eslint+prettier+stylelint+commitlint
    安卓面经_安卓基础面全解析<4/30>之内容提供者全解析
  • 原文地址:https://blog.csdn.net/yqymzj/article/details/126285239