0

I am new to Go and am designing a website using Go. I wish to use multiple templates, such as a base template to incorporate with other templates , such as index. I would like to all the template parsing when the app first starts up. At the moment , I have base.html, footer.html and index.html. I wish to serve index.html which uses base.html and footer.html as well. At the moment , the only response I'm getting from the server is a single newline in a 200 HTTP response verified by wireshark. Anyways, here are my files:

main.go


    package main
      
    import (
        "html/template"
        "log"
        "net/http"
    )
    
    type Initial struct {
        Data string
    }
    
    var cached_templates = template.Must(template.ParseFiles("templates/base.html",
                                                             "templates/footer.html",
                                                             "templates/index.html"))
    
    func renderInitialTemplate(w http.ResponseWriter, _template string, data *Initial) {
        err := cached_templates.ExecuteTemplate(w, _template, data)
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
    }
    
    func indexHandler(w http.ResponseWriter, r *http.Request) {
        data := &Initial{Data: "Bob"}
        renderInitialTemplate(w, "index.html", data)
    }
    
    func main() {
        http.HandleFunc("/", indexHandler)
        log.Fatal(http.ListenAndServe(":80", nil))
    }

index.html - https://pastebin.com/LPy0Xb2Z

footer.html - https://pastebin.com/vVenX4qE

base.html - https://pastebin.com/1jKxv7Uz

I appreciate any help. Thanks.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
buckc
  • 31
  • 2
  • 6

1 Answers1

3

html/template has an option for your problem. you can use template like this:

main.html

{{define "main"}}
<!DOCTYPE html>
<html lang="en">
<body>
{{template "header" .}}

{{template "content" .}}

{{template "footer" .}}
</body>
</html>
{{end}}

header.html

{{define "header"}}
// some header codes or menu or etc.
{{end}}

footer.html

{{define "footer"}}
// some header codes or menu or etc.
{{end}}

for rendreding index page you can do like this :

tmpl, err := template.New("").ParseFiles("index.html", "main.html")
if err != nil {
    panic(err.Error())
}
err = tmpl.ExecuteTemplate(w, "main", whateverContext)
ttrasn
  • 4,322
  • 4
  • 26
  • 43
  • Thanks for taking the time to respond @ttrasn. When I get a chance later this afternoon/night, I'll definitely try this out and update here with my results. It looks like from your answer, I'm going to have to parse the templates every time I use them, correct? I was basically following a tutorial ( https://golang.org/doc/articles/wiki/ ) where one just has to parse all templates once when app starts up and then just execute the template when needed, but if parsing on each request is the proper solution, so be it. Thanks again. – buckc Aug 17 '20 at 14:10