2.HTML渲染 使用LoadHTMLGlob加载html文件,使用get/post方法从html文件里获取前端数据。 controller.IndexController函数为: funcIndexController(c *gin.Context) { c.HTML(http.StatusOK,"login.html", gin.H{"Title":"开元十年", }) } 这个函数表示为,
c.Header("site", "Fossen") } 二、Gin渲染HTML模板 在Gin中默认使用Go语言内置的html/template包处理HTML模板。 1、创建HTML模板文件index.tmpl。 <!-- index.tmpl --><!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>{{.Title}}</title></head><body><h1>{{.Message}}</h1...
r.LoadHTMLGlob("templates/*") 1. 2. 渲染模板 // c.HTML 渲染模板 r.GET("/index", func(c *gin.Context) { c.HTML(http.StatusOK, "index.html", gin.H{"title": "前台首页"}) }) r.GET("/goods", func(c *gin.Context) { c.HTML(http.StatusOK, "goods.html", gin.H{"title":...
在第18到22行,我们告诉Gin路由接受URL路径/上的HTTP GET方法请求。当收到请求时,Gin发送一个HTTP OK状态消息,并用gin.H{}括号内提供的数据渲染index.html模板。在这种情况下,数据只包括一个键/值对,键名为content。在第24到28行,与上面类似,我们告诉Gin路由接受/about路径上的HTTP GET方法请求。这次渲染...
// HTML渲染, router := gin.Default() //router.LoadHTMLFiles("templates/index.html", "templates/login.html") //router.LoadHTMLGlob("templates/*") // 使用不同目录下名称相同的模板 router.LoadHTMLGlob("templates/**/*") router.GET("/users/index", func(context *gin.Context) { context.HT...
在html中我们可以使用特殊的双花括号来渲染title这个值 <html> <h1> {{ .title }} </h1> </html> 值得注意的是这种方式并不是gin特有的,而是golang特有的,它还有其他的模板语法。 模板语法: 定义变量: {{$article := "hello"}} 也可以给变量赋值 ...
Golang框架Gin入门实战–(3)HTML模板渲染及模板语法(上) package main import "github.com/gin-gonic/gin" type Article struct { Title string Content string } func main() { r :...
你可以使用自定义的 html 模板渲染 import"html/template" funcmain() { router:=gin.Default() html:=template.Must(template.ParseFiles("file1","file2")) router.SetHTMLTemplate(html) router.Run(":8080") } 自定义分隔符 你可以使用自定义分隔 ...
在聊Gin的HTML渲染之前,先聊聊Golang(Go语言)内置的html/template,因为Gin的HTML渲染就是基于html/template实现的。 funcmain(){r:=gin.Default()r.GET("/html",func(c*gin.Context){c.Status(200)consttemplateText=`微信公众号:{{printf"%s".}}`tmpl,err:=template.New("htmlTest").Parse(templateText...