Gin: c.HTML can't find template files in Two level directory

Created on 4 Mar 2016  路  2Comments  路  Source: gin-gonic/gin

I need help,the gin freamework example is like this

func main() {
    router := gin.Default()
    router.LoadHTMLGlob("templates/**/*")
    router.GET("/posts/index", func(c *gin.Context) {
        c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{
            "title": "Posts",
        })
    })
    router.GET("/users/index", func(c *gin.Context) {
        c.HTML(http.StatusOK, "users/index.tmpl", gin.H{
            "title": "Users",
        })
    })
    router.Run(":8080")
}

my Directory is

|------templates
             |----users
                     |-----index.tmpl
                     |-----test.tmpl
             |-----posts
                     |-----index.tmpl

I'm use the same like example , but I can't use c.HTML(http.StatusOK, "users/index.tmpl", gin.H{}) for find the template,it says

html/template: "users/index.tmpl" is undefined

but i use c.HTML(http.StatusOK, "test.tmpl", gin.H{}) find it successfully
can you help me .Please forgive my poor English.soory

Most helpful comment

Take a look at: https://github.com/gin-gonic/gin/blob/develop/README.md#L484
add define in users/index.tmpl: {{ define "posts/index.tmpl" }} ... {{ end }}

All 2 comments

Take a look at: https://github.com/gin-gonic/gin/blob/develop/README.md#L484
add define in users/index.tmpl: {{ define "posts/index.tmpl" }} ... {{ end }}

Yep. I've also met with this problem, solved with @aeroxlv 's solution. Templates should be pre-defined, whether they end with tmpl or html.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ccaza picture ccaza  路  3Comments

mastrolinux picture mastrolinux  路  3Comments

rawoke083 picture rawoke083  路  3Comments

Bloomca picture Bloomca  路  3Comments

xpbliss picture xpbliss  路  3Comments