Home > Enterprise >  gin HTMLGlob not applying **
gin HTMLGlob not applying **

Time:07-12

I have a gin v1.8.1 go program that uses templates. Main go file is cmd/app/app.go. Inside it, I have this

    r := gin.Default()

    cwd, err := os.Getwd()
    if err != nil {
        panic(err)
    }
    fmt.Printf("CWD is %s", cwd)
    r.LoadHTMLGlob("../../templates/**/*")

When I run I get this panic

CWD is /Users/paul/src/personal/app/cmd/app
panic: html/template: pattern matches no files: `../../templates/**/*`

goroutine 1 [running]:
html/template.Must(0x0, {0x4aac5a0, 0xc000584490})
    /usr/local/Cellar/go/1.18.3/libexec/src/html/template/template.go:374  0x8b
github.com/gin-gonic/gin.(*Engine).LoadHTMLGlob(0xc0005b04e0, {0x4a2e2ae, 0x14})
    /Users/paul/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:251  0x15e
main.main()
    /Users/paul/src/personal/app/cmd/app/app.go:31  0x169

When I remove the double star (r.LoadHTMLGlob("../../templates/*")), it works fine. I know the standard library globber does not handle double star, but gin docs says theirs does

CodePudding user response:

Apparently I have just misunderstood how this implementation of globbing works. I added a new template, test/index.html and tried the syntax ../../templates/**/*. Gin running in debug mode lists all of the templates it finds. Rather than listing the templates in the base directory, it only finds index.html. The ** means match a directory that must exist.

  • Related