I am trying to use html/template package to display a table of information in a web page in Golang. The current implementation of the template I have outputs to standard out.
I want to be able to pass the page content generated by the template to another function that will render my web page.
How to achieve this?
Explanation of code attached below. Basically, I want to capture the result of t.Execute(os.Stdout, persons) where persons is a slice of string. I know that t.Execute only returns an error. But it prints to standard out the template t along with the person name values. I wanted to capture the combined template and person array texts into a variable instead. I would like to assign the content to the body variable in my code below.
t := template.Must(template.New("").Parse(`<table>{{range .}}<tr><td>{{.}}</td></tr>{{end}}</table>`))
if err := t.Execute(os.Stdout, persons); err != nil {
log.Fatal(err)
}
title := "User Info Page"
p := &Page{Title: title, Body: []byte(body)}
renderTemplate(w, "view", p)
}