From 8e4a37b558e87a37691fc0082598f41896b83148 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Wed, 15 Aug 2012 11:51:25 +0200 Subject: [PATCH] Organize code: move template stuff to it's own file --- template.go | 28 ++++++++++++++++++++++++++++ trantor.go | 23 ----------------------- 2 files changed, 28 insertions(+), 23 deletions(-) create mode 100644 template.go diff --git a/template.go b/template.go new file mode 100644 index 0000000..2c3dd68 --- /dev/null +++ b/template.go @@ -0,0 +1,28 @@ +package main + +import ( + "html/template" + "net/http" +) + +func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) { + // TODO: when finish devel conver to global: + var templates = template.Must(template.ParseFiles("head.html", "foot.html", "front.html", "book.html", "search.html", "upload.html")) + + // TODO: use includes + err := templates.ExecuteTemplate(w, "head.html", nil) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + err = templates.ExecuteTemplate(w, tmpl+".html", data) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + err = templates.ExecuteTemplate(w, "foot.html", nil) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} diff --git a/trantor.go b/trantor.go index 21d852d..9e5a950 100644 --- a/trantor.go +++ b/trantor.go @@ -1,7 +1,6 @@ package main import ( - "html/template" "labix.org/v2/mgo" "labix.org/v2/mgo/bson" "net/http" @@ -13,28 +12,6 @@ const ( BOOKS_COLL = "books" ) -func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) { - // TODO: when finish devel conver to global: - var templates = template.Must(template.ParseFiles("head.html", "foot.html", "front.html", "book.html", "search.html", "upload.html")) - - // TODO: use includes - err := templates.ExecuteTemplate(w, "head.html", nil) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - err = templates.ExecuteTemplate(w, tmpl+".html", data) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - err = templates.ExecuteTemplate(w, "foot.html", nil) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } -} - func bookHandler(coll *mgo.Collection, w http.ResponseWriter, r *http.Request) { var book Book coll.Find(bson.M{"title": r.URL.Path[len("/book/"):]}).One(&book)