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

LoadHTMLTemplates not work! #145

Closed
train860 opened this issue Nov 8, 2014 · 3 comments
Closed

LoadHTMLTemplates not work! #145

train860 opened this issue Nov 8, 2014 · 3 comments

Comments

@train860
Copy link

train860 commented Nov 8, 2014

hi!
here is my code.

func main() {
    gin.SetMode(gin.DebugMode)
    r := gin.Default()
    r.LoadHTMLTemplates("templates/*")
    r.GET("/", controllers.AddMember())
    r.GET("/member/:nick", controllers.GetMemberByNick())
    r.GET("/html", func(c *gin.Context) {
        obj := gin.H{"title": "Main website"}
        c.HTML(200, "templates/index.tmpl", obj)
    })
    r.Run(":8080")
}

here is GetMemberByNick():

func GetMemberByNick() gin.HandlerFunc {
    return func(c *gin.Context) {
        m := services.GetMemberByNick(c)
        if m != nil {
            c.HTML(200, "index.tmpl", m)
        } else {
            msg["status"] = "0"
            msg["info"] = "not found"
            c.JSON(200, msg)
        }

    }
}

here is my project

d69c1268-a08b-460a-aa41-87dc246202b9

but,I cannot get the template("/member/:nick" and "/html")

can you help? thanks.

@kuxuxun
Copy link

kuxuxun commented Nov 9, 2014

it might be same as #120 #130 and #134

@boboTjones
Copy link

I am having this same issue. Apologies in advance. I have forgotten how to do pretty github markdown.

Just to test, I put

fmt.Println(os.Getwd())

in the handler and it returned the base path where the server is run (/Users/erin/codebase/bangorang, no trailing /). So I tried various permutations of ./ and /full/path/to/file, and I see

Error #1: html/template: "/Users/erin/codebase/bangorang/src/github.com/mycode/myproject/templates/test.tmpl" is undefined

I copied the file test.tmpl into /Users/erin/bangorang and sure enough, it worked. This prompted me to start digging around in html/template -- I think the problem is around here:

http://golang.org/src/pkg/html/template/template.go?s=9766:9835#L324

More investigation required. Just wanted to make noise.

@boboTjones
Copy link

If you look at this line:

https://github.com/gin-gonic/gin/blob/develop/render/render.go#L122

This assumes that the file path is absolute. However, if I try to give a complete path as a string to this, I see

Error #01: html/template: "/Users/erin/codebase/bangorang/src/github.com/myorg/myproject/templates/test.tmpl" is undefined 

See http://golang.org/src/html/template/template.go?s=9766:9835#L98

The relevant bits of my code are here. I can create a template object and print the HTML (I was trying to figure out some way to just massage it into the server response, but I'm not there yet):

https://gist.github.com/boboTjones/cfce4394a4bc246a33e2

If I run the binary from the template directory, using the command below, the server finds the template and serves the correct response.

lollipop:templates erin$ go install github.com/bangorang/myproject && /Users/erin/codebase/bangorang/bin/myproject -r

Which is what I'm going to do, going forward, assuming this is how gin-gonic is meant to work.

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

No branches or pull requests

4 participants