just get the rendered post-HTML content, save it to a string variable, and do not output
just get the rendered post-HTML content, save it to a string variable, and do not output
look at the function signatures of template rendering: Execute (wr io.Writer, data interface {}) error
obviously the rendered content can be output to anywhere that implements the io.Writer
interface, such as os.Stdout
, files, buffer, etc.
type User struct {
Name string
}
func main() {
tpl := template.New("example")
tpl, _ = tpl.Parse(" hello {{.Name}}
")
data := User{Name: "Tom"}
var buf bytes.Buffer
if err := tpl.Execute(&buf, data); err != nil {
log.Fatal(err)
}
fmt.Println(buf.String()) // // hello Tom
}