gin html模板渲染,Go 的gin 框架 和 gorm 和 html/template库

Gin 基础 :

Gin 的hello world :

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

package mainimport("github.com/gin-gonic/gin"

"net/http")

func main() {

engine :=gin.Default()

engine.GET("/", func(context *gin.Context) {

context.String(http.StatusOK,"hello world!")

})

engine.Run()

}

View Code

Gin 的 context.Params(path 参数) :

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

package mainimport("fmt"

"github.com/gin-gonic/gin"

"net/http")

func main() {

engine :=gin.Default()

engine.GET("/:name/*rest", func(context *gin.Context) {

context.String(http.StatusOK,"hello world!")

fmt.Println(context.Params)

params :=context.Params

fmt.Printf("%T", params)

ret,ok := params.Get("name")ifok{

fmt.Println(ret)

}

fmt.Println(params[0])

})

engine.Run()

}

View Code

Gin 的 context.Query (get 参数):

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

package mainimport("fmt"

"github.com/gin-gonic/gin"

"net/http")

func main() {

engine :=gin.Default()

engine.GET("/", func(context *gin.Context) {

context.String(http.StatusOK,"hello world!")

fmt.Println(context.Query("name")) //如果不存在返回 空串

fmt.Println(context.DefaultQuery("name","none"))//如果不存在返回 none

})

engine.Run()

}

View Code

Gin 的 context.PostForm(post 参数):

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

package mainimport("fmt"

"github.com/gin-gonic/gin"

"net/http")

func main() {

engine :=gin.Default()

engine.POST("/", func(context *gin.Context) {

context.String(http.StatusOK,"hello world")

fmt.Println(context.PostForm("name"))

fmt.Println(context.DefaultPostForm("name","none"))

})

engine.Run()

}

View Code

Gin 的HTML渲染:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

package mainimport("github.com/gin-gonic/gin"

"html/template"

"net/http")

func main() {

eng :=gin.Default()

eng.SetFuncMap(template.FuncMap{"safe": func(s string)template.HTML {returntemplate.HTML(s)

},

})

eng.LoadHTMLGlob("templates/**/*")

eng.GET("/test01", func(context *gin.Context) {

context.HTML(http.StatusOK,"test/test01.html",gin.H{"a":`百度一下`,

})

})

eng.Run()

}

main.go

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

{{define "test/test01.html"}}

posts/index

hello test01

{{ .a|safe }}

{{end}}

templates/test/test01.html

注意:使用自定义模板函数的时候要先 SetFuncMap 后  LoadHTMLGlob

当HTML文件中引用了静态文件时:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

package mainimport("github.com/gin-gonic/gin"

"html/template"

"net/http")

func main() {

eng :=gin.Default()

eng.SetFuncMap(template.FuncMap{"safe": func(s string)template.HTML {returntemplate.HTML(s)

},

})

eng.Static("/xxx","./static")

eng.LoadHTMLGlob("templates/**/*")

eng.GET("/test01", func(context *gin.Context) {

context.HTML(http.StatusOK,"test/test01.html",gin.H{"a":`百度一下`,

})

})

eng.Run()

}

main.go

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

{{define "test/test01.html"}}

posts/index

hello test01

{{ .a | safe }}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值