Ejemplo n.º 1
0
func superHandler(w http.ResponseWriter, r *http.Request) {
	t := template.Must(template.ParseGlob("template/*")) // add sub-templates in /template
	t.ParseFiles("super.html")                           //parse super
	ctx := appengine.NewContext(r)

	cKeys, err := m.ParseAll(ctx, m.Category{}) // get category keys
	if err != nil {
		panic(err)
	}
	cModels, err := m.ParseEntitiesFromKeys(ctx, cKeys) // get category model as datamodel interface
	if err != nil {
		panic(err)
	}
	var Cvms []m.Category // convert from interface to struct
	for _, model := range cModels {
		Cvms = append(Cvms, model.(m.Category))
	}

	lKeys, err := m.ParseAll(ctx, m.Location{}) // same procedure
	if err != nil {
		panic(err)
	}
	lModels, err := m.ParseEntitiesFromKeys(ctx, lKeys)
	if err != nil {
		panic(err)
	}
	var Lvms []m.Location
	for _, model := range lModels {
		Lvms = append(Lvms, model.(m.Location))
	}
	err = t.ExecuteTemplate(w, "base",
		struct {
			CVMS []m.Category
			LVMS []m.Location
		}{Cvms, Lvms})
	if err != nil {
		w.Write([]byte(err.Error()))
	}
}
Ejemplo n.º 2
0
func handler(w http.ResponseWriter, r *http.Request) {
	t := template.Must(template.ParseGlob("template/*")) // add sub-templates in /template
	t.ParseFiles("index.html")                           // parse main.html as main
	ctx := appengine.NewContext(r)
	keys, err := m.ParseAll(ctx, m.Post{}) // get all posts in db.
	if err != nil {
		w.Write([]byte(err.Error()))
	}
	dataModels, err := m.ParseEntitiesFromKeys(ctx, keys) // get DataModel Entities
	if err != nil {
		w.Write([]byte(err.Error()))
	}
	var postModels []m.Post
	for i, model := range dataModels { // convert to ViewModel
		p := model.(m.Post)
		p.ID = keys[i].IntID()
		postModels = append(postModels, p)
	}
	err = t.ExecuteTemplate(w, "base", postModels) // exec templates
	if err != nil {
		w.Write([]byte(err.Error()))
	}
}