Пример #1
0
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",
	})

}
Пример #2
0
func init() {
	var err error
	tpls, err = template.ParseFiles("assets/templates/index.gohtml")
	if err != nil {
		log.Fatalln(err)
	}
	http.HandleFunc("/", handleIndex)
}
Пример #3
0
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)
	}
}
Пример #4
0
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)
	}
}