Go: 提供静态模板服务

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

Go: serve static templates

问题

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

Go目录结构

  1. src
  2. /github.com
  3. /sam
  4. /hello
  5. auth.go
  6. main.go
  7. /templates
  8. signup.html

auth.go

  1. package main
  2. //...
  3. func homeHandler(w http.ResponseWriter, r *http.Request) {
  4. renderTemplate(w, "signup", nil)
  5. }
  6. func renderTemplate(w http.ResponseWriter, tmpl string, user *data.User) {
  7. t := template.Must(template.New("tele").ParseFiles("templates/" + tmpl + ".html"))
  8. err := t.ExecuteTemplate(w, tmpl, user)
  9. if err != nil {
  10. http.Error(w, err.Error(), http.StatusInternalServerError)
  11. }
  12. }

main.go

  1. package main
  2. func main() {
  3. //...
  4. http.Handle("/templates/", http.StripPrefix("/templates/", http.FileServer(http.Dir("templates"))))
  5. //...
  6. }

signup.html

  1. {{ define "signup" }}
  2. //html代码
  3. {{ end }}

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

  1. 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

  1. src
  2. /github.com
  3. /sam
  4. /hello
  5. auth.go
  6. main.go
  7. /templates
  8. signup.html

auth.go

  1. package main
  2. //...
  3. func homeHandler(w http.ResponseWriter, r *http.Request) {
  4. renderTemplate(w, "signup", nil)
  5. }
  6. func renderTemplate(w http.ResponseWriter, tmpl string, user *data.User) {
  7. t := template.Must(template.New("tele").ParseFiles("templates/" + tmpl + ".html"))
  8. err := t.ExecuteTemplate(w, tmpl, user)
  9. if err != nil {
  10. http.Error(w, err.Error(), http.StatusInternalServerError)
  11. }
  12. }

main.go

  1. package main
  2. func main() {
  3. //...
  4. http.Handle("/templates/", http.StripPrefix("/templates/", http.FileServer(http.Dir("templates"))))
  5. //...
  6. }

signup.html

  1. {{ define "signup" }}
  2. //html code
  3. {{ end }}

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

  1. 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:

确定