0

I'm currently trying to create a small web application using golang and I'm following the tutorial on their webpage (https://golang.org/doc/articles/wiki/final.go). Instead of having the templates on the same folder as the rest of the code I'm trying to move them into templates/template_name.html.

For template rendering I'm using the following code:

var templates = template.Must(template.ParseFiles("templates/edit.html", "templates/view.html"))

func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
    err := templates.ExecuteTemplate(w, "templates/"+tmpl+".html", p)
    if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}

And for example my View handler is the following:

func viewHandler(w http.ResponseWriter, r *http.Request, title string) {                                                                                                                                    
    p, err := loadPage(title)                                                                                                                                                                           
    if err != nil {                                                                                                                                                                                     
            http.Redirect(w, r, "/edit/"+title, http.StatusFound)                                                                                                                                       
            return                                                                                                                                                                                      
    }                                                                                                                                                                                                   
    renderTemplate(w, "view", p)                                                                                                                                                                        

}

I have the templates/ folder with the edit.html and view.html file inside. I run the full code with: go run wiki.go but when I try to access the webpage I get the following error:

html/template: "templates/view.html" is undefined

Any idea of what might be happening?

Cláudio Ribeiro
  • 1,509
  • 3
  • 37
  • 65
  • 2
    Read the documentation of ParseFiles. The name of the template is _not_ the filepath. So fix `"templates/"+tmpl+".html"` – Volker Sep 25 '18 at 14:28
  • I've read the documentation, but I don't understand how it can reach the template inside the folder without indicating it. I'm a little bit lost. – Cláudio Ribeiro Sep 25 '18 at 14:36
  • 1
    As @Volker wrote, the template name is just `"view.html"`, and not `"templates/view.html"`. Please see related / possible duplicate: [Go template name](https://stackoverflow.com/questions/41176355/go-template-name/41187671#41187671). – icza Sep 25 '18 at 14:39
  • 1
    Did you try a simple "edit.html"? – Volker Sep 25 '18 at 14:39
  • I just tried a simple edit.html and I still get the same error which is strange because I don't have '/templates' on any part of the code but it still references it. – Cláudio Ribeiro Sep 25 '18 at 14:43
  • 1
    make sure you save the code, run `go clean`, re-build the code and run it. – leaf bebop Sep 25 '18 at 14:49
  • It's working now, thanks for the tips! – Cláudio Ribeiro Sep 25 '18 at 14:50

1 Answers1

0

Like @Volker said, we use the template's path in ParseFiles and the filename in ExecuteTemplate:

var templates = template.Must(template.ParseFiles("templates/edit.html", "templates/view.html"))

func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
    err := templates.ExecuteTemplate(w, tmpl+".html", p)
    if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}

My implementation: https://github.com/librity/gowiki

ליאור
  • 21
  • 2