Description

The LoadHTMLGlob("templates/**/*") example in the GitHub documentation doesn't work.

How to reproduce

Create template files:

$ mkdir -p templates/posts templates/users
$ echo "posts/index.tmpl" > templates/posts/index.tmpl
$ echo "users/index.tmpl" > templates/users/index.tmpl

Go code (from example in documentation):

package main

import (
        "net/http"

    "github.com/gin-gonic/gin"
)

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")
}

Expectations

$ go run main.go
[GIN-debug] Loaded HTML Templates (2): 
    - posts/index.tmpl
    - users/index.tmpl
$ curl http://localhost:8080/posts/index
posts/index.tmpl
$ curl http://localhost:8080/users/index
users/index.tmpl

Actual result

$ go run main.go
[GIN-debug] Loaded HTML Templates (2): 
        - index.tmpl
    - 
$ curl http://localhost:8080/posts/index
$ curl http://localhost:8080/users/index

Also, accessing http://localhost:8080/posts/index and http://localhost:8080/users/index throws an error server-side:

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

Environment

  • go version: 1.13.14
  • gin version (or commit ref): 1.6.3
  • operating system: MacOS 10.15.6

Comment From: favowang

Write you html like this: {{ define "index/index.html" }} html body {{ end }}

Comment From: amorey

I see, I expected the name of the template to come from the file name. Is it possible to do template inheritance when defining templates like this?

Comment From: favowang

I'm a new player and can't answer your question. Maybe you can have a try.

Comment From: mnvx

I have same problem. Issue is closed but no info provided about how to fix it?

Comment From: ghost

I have same problem. Issue is closed but no info provided about how to fix it?

try what favowang said

Comment From: scippio

crazy...

Comment From: tianxin-zh

you should to check file suffix is html ,not tmpl