从0开始学go第四天
模板继承
继承根模板,重新定义“块模板”
【Go Web开发系列教程】07-Go模板继承_哔哩哔哩_bilibili
解析模板时,base模板要在前
渲染模板时:
要用ExecuteTemplate,而不是Excute
模板补充:Go语言标准库之http/template | 李文周的博客 (liwenzhou.com)
如何给vscode中的tmpl文件添加高亮?
[Go]在vscode中添加对模板文件tmpl的html语法自动补全的支持_郭老二的博客-CSDN博客
这个问题困扰我好多天了,up用的goland自动高亮,vscode就没有,得自己加
我的是直接点添加项
gin框架模板渲染
打开一个模板文件用:LoadHTMLFiles()
package mainimport ("net/http""github.com/gin-gonic/gin"
)func main() {//定义模板r := gin.Default()//解析模板r.LoadHTMLFiles("templates/index.tmpl")//渲染模板r.GET("/index", func(c *gin.Context) {//返回一个渲染好的HTML文件//HTTP请求c.HTML(http.StatusOK, "index.tmpl", gin.H{"title": "蒋志宇真帅",})})r.Run(":8090") //启动server
}
多个用:LoadHTMLGlob()
func main() {r := gin.Default()r.LoadHTMLGlob("templates/**/*")//r.LoadHTMLFiles("templates/posts/index.html", "templates/users/index.html")r.GET("/posts/index", func(c *gin.Context) {c.HTML(http.StatusOK, "posts/index.html", gin.H{"title": "posts/index",})})r.GET("users/index", func(c *gin.Context) {c.HTML(http.StatusOK, "users/index.html", gin.H{"title": "users/index",})})r.Run(":8080")
}