Execute template with a parameter

This works

tpl.ExecuteTemplate(w, "index.html", data)

But sending a parameter this does not work. How can I get this to work?

page := template.HTML("index.html")
tpl.ExecuteTemplate(w, page, data)

./main.go:84:27: cannot use page (type func(http.ResponseWriter, *http.Request)) as type string in argument to tpl.ExecuteTemplate

What happens? Do you get an error?

1 Like

I think I found the solution. It was maybe variable scope

path := strings.Trim(r.URL.Path, "/")

var page string  <--- variable scope?
switch path {
case "":
	page = "index.html"
	fmt.Println(page, list)
	page = path + ".html"
	fmt.Println(page, list)

err = tpl.ExecuteTemplate(w, page, list)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.