Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LoadHTMLGlob("templates/**/*") example in documentation doesn't work #2444

Closed
amorey opened this issue Jul 27, 2020 · 6 comments
Closed

LoadHTMLGlob("templates/**/*") example in documentation doesn't work #2444

amorey opened this issue Jul 27, 2020 · 6 comments

Comments

@amorey
Copy link

amorey commented Jul 27, 2020

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
@favowang
Copy link

favowang commented Aug 6, 2020

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

@amorey amorey closed this as completed Aug 6, 2020
@amorey amorey reopened this Aug 6, 2020
@amorey
Copy link
Author

amorey commented Aug 6, 2020

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?

@favowang
Copy link

favowang commented Aug 6, 2020

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

@amorey amorey closed this as completed Aug 7, 2020
@mnvx
Copy link

mnvx commented Sep 23, 2020

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

@ghost
Copy link

ghost commented Feb 6, 2021

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

try what favowang said

sananand007 added a commit to timwangmusic/Vacation-Planner that referenced this issue Feb 21, 2021
@scippio
Copy link

scippio commented Apr 11, 2023

crazy...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants