• With issues:
  • Use the search tool before opening a new issue.
  • Please provide source code and commit sha if you found a bug.
  • Review existing issues and provide feedback or react to them.

Description

I have templates:

Gin Can't use template from root if load nested templates

With content like https://gin-gonic.com/docs/examples/html-rendering/

If I use router.LoadHTMLGlob("templates/**/*.tmpl") then /index endpoint don't work. If I use router.LoadHTMLGlob("templates/*.tmpl") works only /index endpoint.

How to reproduce

package main

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

func main() {
    router := gin.Default()

    router.LoadHTMLGlob("templates/**/*.tmpl")

    router.GET("/index", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.tmpl", gin.H{
            "title": "website",
        })
    })

    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(":9000")
}

And see in console:

[GIN-debug] Loaded HTML Templates (4): 
    - 
    - index.tmpl
    - posts/index.tmpl
    - users/index.tmpl

Loaded 4 templates. But I have only 3 template files. Why? https://github.com/gin-gonic/gin/issues/2704

And I make 3 GET requests:

/posts/index /users/index /index

But panic on /index

Expectations

$ curl -i http://localhost:9000/index
website
$ curl -i http://localhost:9000/posts/index
Posts
Using posts/index.tmpl
$ curl -i http://localhost:9000/users/index
Users
Using users/index.tmpl

Actual result

$ curl -i http://localhost:9000/index
empty response with 200 code, Content-Length: 0
$ curl -i http://localhost:9000/posts/index
Posts
Using posts/index.tmpl
$ curl -i http://localhost:9000/users/index
Users
Using users/index.tmpl

Environment

  • go version: 1.16.3
  • gin version (or commit ref): I ran today go get -u github.com/gin-gonic/gin. Nowhere was it written which version was installed.
  • operating system: WIndows 10

Comment From: praveenpenumaka

Accoridng to go documentation. Hence it is unable to parse

The files are matched according to the semantics of filepath.Match

Comment From: leungyauming

Screenshot 2021-04-29 at 12 58 33 PM

According to the documentation of filepath.Glob function which is used by the LoadHTMLGlob function of gin. The pattern for matching files is the same with filepath.Match function.

Screenshot 2021-04-29 at 12 57 51 PM

According to the documentation of filepath.Match function, the above pattern are supported. And I think ** is not a pattern supported.

Comment From: srvmux

so can we get a proper answer on whether we can load and use templates from subdirectories or not? if yes then how? it seems that there's no direct or proper answers about this issue.

Comment From: srvmux

Screenshot 2021-04-29 at 12 58 33 PM

According to the documentation of filepath.Glob function which is used by the LoadHTMLGlob function of gin. The pattern for matching files is the same with filepath.Match function.

Screenshot 2021-04-29 at 12 57 51 PM

According to the documentation of filepath.Match function, the above pattern are supported. And I think ** is not a pattern supported.

I've tried both ** and * and even tried loading the files explicitly and still cannot import templates within subdirectories?

Comment From: mbretter

same here, the directory structure:

 ls -l templates/**/*.html
-rw-rw-r-- 1 xx xx 667 Mai 25 11:44 templates/generic/ga4.html
-rw-rw-r-- 1 xx xx 667 Mai 25 11:44 templates/tactix/ga4.html

templates are loaded with:

r.LoadHTMLGlob("templates/**/*.html")

Gin-Debug shows on startup:

[GIN-debug] Loaded HTML Templates (2): 
        - 
        - ga4.html

only one template, probably the last one found was loaded.

It should work exactly as written in the examples, under "Using templates with same name in different directories": https://gin-gonic.com/docs/examples/html-rendering/

Comment From: mbretter

I have found the issue, in my case the

{{ define "generic/ga4.html" }}

was missing in the template files.

It is working now, but at startup gin-gonic displays this:

[GIN-debug] Loaded HTML Templates (4): 
        - 
        - ga4.html
        - generic/ga4.html
        - tactix/ga4.html

the empty template still exists and the additional ga4.html looks weird, because there is no ga4.html in the root-template folder.