Organize code: move template stuff to it's own file
This commit is contained in:
parent
b9e39cf57f
commit
8e4a37b558
2 changed files with 28 additions and 23 deletions
28
template.go
Normal file
28
template.go
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
23
trantor.go
23
trantor.go
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
|
||||||
"labix.org/v2/mgo"
|
"labix.org/v2/mgo"
|
||||||
"labix.org/v2/mgo/bson"
|
"labix.org/v2/mgo/bson"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -13,28 +12,6 @@ const (
|
||||||
BOOKS_COLL = "books"
|
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) {
|
func bookHandler(coll *mgo.Collection, w http.ResponseWriter, r *http.Request) {
|
||||||
var book Book
|
var book Book
|
||||||
coll.Find(bson.M{"title": r.URL.Path[len("/book/"):]}).One(&book)
|
coll.Find(bson.M{"title": r.URL.Path[len("/book/"):]}).One(&book)
|
||||||
|
|
Reference in a new issue