func handleSomeRoute(res http.ResponseWriter, req *http.Request) { // Step #1: Parse template tpl, err := template.ParseFiles( "assets/templates/index.gohtml", "assets/templates/login.gohtml", ) if err != nil { http.Error(res, err.Error(), 500) return } type MyModel struct { SomeField int Values []int Data string } // Step #2: Execute template tpl.ExecuteTemplate(res, "assets/templates/login.gohtml", MyModel{ SomeField: 123, Values: []int{1, 2, 3, 4, 5}, Data: "Some Value", }) }
func init() { var err error tpls, err = template.ParseFiles("assets/templates/index.gohtml") if err != nil { log.Fatalln(err) } http.HandleFunc("/", handleIndex) }
func showProfile(res http.ResponseWriter, req *http.Request, params httprouter.Params) { tpl, err := template.ParseFiles("templates/templates.gohtml") if err != nil { panic(err) } err = tpl.ExecuteTemplate(res, "edit-form", nil) if err != nil { http.Error(res, err.Error(), 500) } }
func showProfile(res http.ResponseWriter, req *http.Request, params httprouter.Params) { tpl, err := template.ParseFiles("templates/templates.gohtml") if err != nil { panic(err) } ctx := appengine.NewContext(req) u := user.Current(ctx) key := datastore.NewKey(ctx, "Profile", u.Email, 0, nil) var profile Profile err = datastore.Get(ctx, key, &profile) if err != nil { profile.Email = u.Email } err = tpl.ExecuteTemplate(res, "edit-form", profile) if err != nil { http.Error(res, err.Error(), 500) } }