Go: 提供静态模板服务

huangapple go评论82阅读模式
英文:

Go: serve static templates

问题

我似乎无法提供静态模板服务。这是我的代码:

Go目录结构

src
     /github.com
          /sam
               /hello
                   auth.go
                   main.go
                   /templates
                       signup.html

auth.go

package main
//...
func homeHandler(w http.ResponseWriter, r *http.Request) {
    renderTemplate(w, "signup", nil)
}

func renderTemplate(w http.ResponseWriter, tmpl string, user *data.User) {
    t := template.Must(template.New("tele").ParseFiles("templates/" + tmpl + ".html"))
    err := t.ExecuteTemplate(w, tmpl, user)
    if err != nil {
	    http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}

main.go

package main

func main() {
    //...
    http.Handle("/templates/", http.StripPrefix("/templates/", http.FileServer(http.Dir("templates"))))
    //...
}

signup.html

{{ define "signup" }}
//html代码
{{ end }}

我运行了go install github.com/sam/auth并打开了localhost:3000,但仍然出现了恐慌错误:

open templates/signup.html: no such file or directory

为什么会这样?

英文:

I can't seem to get the static templates served. Here's my code

Go Directory Structure

src
     /github.com
          /sam
               /hello
                   auth.go
                   main.go
                   /templates
                       signup.html

auth.go

package main
//...
func homeHandler(w http.ResponseWriter, r *http.Request) {
    renderTemplate(w, "signup", nil)
}

func renderTemplate(w http.ResponseWriter, tmpl string, user *data.User) {
    t := template.Must(template.New("tele").ParseFiles("templates/" + tmpl + ".html"))
    err := t.ExecuteTemplate(w, tmpl, user)
    if err != nil {
	    http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}

main.go

package main

func main() {
    //...
    http.Handle("/templates/", http.StripPrefix("/templates/", http.FileServer(http.Dir("templates"))))
    //...
}

signup.html

{{ define "signup" }}
//html code
{{ end }}

Ran go install github.com/sam/auth and opened localhost:3000 but I still get the panic error:

open templates/signup.html: no such file or directory

WHY???

答案1

得分: 1

你使用的路径 - templates/ - 是相对于程序运行的位置的。如果你希望它在无论在哪里运行程序时都能正常工作,你应该使用绝对路径,比如 $GOPATH/src/github.com/sam/hello/templates/

但这种方式也很脆弱,因为目录可能会移动,你的程序在另一台机器上可能无法运行。我建议你考虑将资源(模板)与二进制文件捆绑在一起。一个很好的方法是使用 go-bindata

英文:

The path you use - templates/ - is relative to where the program is run. if you want it to work regardless of where you run the program, you should use an absolute path, like $GOPATH/src/github.com/sam/hello/templates/

But this is fragile too, since the directory can move, and your program will not run on another machine. I would suggest you look at bundling your assets (the templates) with the binary. A good way to do that is using go-bindata

huangapple
  • 本文由 发表于 2014年12月11日 22:08:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/27424801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定