HTML

HTML

使用 LoadHTMLGlob() 或 LoadHTMLFiles()

func main() {
    router := gin.Default()
    router.LoadHTMLGlob("templates/*")
    //router.LoadHTMLFiles("templates/template1.html", "templates/template2.html")
    router.GET("/index", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.tmpl", gin.H{
            "title": "Main website",
        })
    })
    router.Run(":8080")
}

templates/index.tmpl

<html>
    <h1>
        {{ .title }}
    </h1>
</html>

使用不同目录下名称相同的模板

templates/posts/index.tmpl

templates/users/index.tmpl

自定义模板

自定义分隔符

自定义模板内置名称

raw.tmpl

输出

Last updated

Was this helpful?