1

Issue details

I am using Go language

Page is blank when trying to access footer.html from a different directory level

My folder structure is like below

Go
   static
       css
           main.css
   templates
       front
           home
               home.html
           footer.html
   main.go

main.go code

func homeHandler(w http.ResponseWriter, r *http.Request) {
    templates = template.Must(template.ParseGlob("templates/front/home/*.html"))
    templates.ExecuteTemplate(w, "home.html", nil)
}

html

{{template "footer.html"}}
Pankaj
  • 9,749
  • 32
  • 139
  • 283

1 Answers1

0

See "Go template name": a template.Template value may be (and usually is) a collection of multiple, associated templates.

Since footer.html is not in home folder, but directly in the front parent folder, you need to parse it too, for its name (footer.html) to be available.
This is illustrated for instance in "How to Render multiple templates".

func homeHandler(w http.ResponseWriter, r *http.Request) {
    templates = template.Must(template.ParseGlob("templates/front/home/*.html"))
    templates = template.Must(templates.ParseGlob("templates/front/*.html"))
    templates.ExecuteTemplate(w, "home.html", nil)
}

The second ParseGlob() is applied to the Template object templates, not the package name template.
I would choose a different name, like websiteTemplates, to avoid any confusion with the package name.

Then your HTML file should work

{{template "footer.html"}}
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250