From fea21c9d10d89f90b71988dcb41722527b947f60 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Thu, 21 May 2015 18:34:14 -0700 Subject: [PATCH] start categories listing page, in the process of reworking the template code a bit --- categories/categories.go | 1 + main.go | 20 ----------------- route_handlers.go | 22 ++++++++++++------- templates.go | 39 +++++++++++++++++++++++++++++++++ templates/layout.html | 1 + templates/pages/categories.html | 27 +++++++++++++++++++++++ templates/pages/list.html | 2 +- 7 files changed, 83 insertions(+), 29 deletions(-) create mode 100644 templates.go create mode 100644 templates/pages/categories.html diff --git a/categories/categories.go b/categories/categories.go index 7f7d865..dcb40e8 100644 --- a/categories/categories.go +++ b/categories/categories.go @@ -59,6 +59,7 @@ func (category *Category) Depth() int { return depth } +// Helper function for templates (add form select list) func (category *Category) ToString() string { return strings.Repeat("- ", category.Depth()) + category.Name } \ No newline at end of file diff --git a/main.go b/main.go index 359d98d..20a3ca7 100644 --- a/main.go +++ b/main.go @@ -6,11 +6,8 @@ import ( "fmt" "github.com/gorilla/sessions" _ "github.com/lib/pq" - "html/template" "net/http" "os" - "path/filepath" - "regexp" "flag" ) @@ -39,7 +36,6 @@ var ( config Config db *sql.DB store = sessions.NewCookieStore([]byte("key-store-auth-secret")) - templates = map[string]*template.Template{} ) func loadConfig(env string) { @@ -71,22 +67,6 @@ func dbConnect() { } } -func initTemplates() { - files, _ := filepath.Glob("templates/pages/*.html") - re := regexp.MustCompile("templates/pages/(.*).html") - fmt.Println("Loading Templates:") - for _, t := range files { - name := re.FindStringSubmatch(t) - fmt.Println(" ", name[1]) - var err error - templates[name[1]], err = template.ParseFiles("templates/layout.html", t) - if err != nil { - fmt.Println("Template load error: ", err) - os.Exit(-1) - } - } -} - func main() { fmt.Println("transmet ", VERSION) diff --git a/route_handlers.go b/route_handlers.go index ee73ca8..456776b 100644 --- a/route_handlers.go +++ b/route_handlers.go @@ -61,14 +61,6 @@ func getPostHandler(getFn, postFn func(http.ResponseWriter, *http.Request)) func } } -func ShowTemplate(template string, w http.ResponseWriter, data map[string]interface{}) { - err := templates[template].Execute(w, data) - if err != nil { - fmt.Println("Exec err: ", err) - } - // TODO: show error 500 page -} - // Log in page handler func LoginFormHandler(w http.ResponseWriter, r *http.Request) { session, _ := store.Get(r, "c_user") @@ -250,6 +242,19 @@ func addedHandler(w http.ResponseWriter, r *http.Request, user *user.User) { ShowTemplate("added", w, map[string]interface{}{"user": user, "flashes": flashes}) } +func categoriesFormHandler(w http.ResponseWriter, r *http.Request, user *user.User) { + session, _ := store.Get(r, "c_user") + flashes := GetFlashes(session) + session.Save(r, w) + categories.LoadCategories(db) + + ShowTemplate("categories", w, map[string]interface{}{"user": user, "flashes": flashes, "categories": categories.CategoriesTree}) +} + +func categoriesPostHandler(w http.ResponseWriter, r *http.Request, user *user.User) { + http.Redirect(w, r, "/categories", http.StatusFound) +} + func init_route_handlers() { http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("js/")))) http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css/")))) @@ -265,6 +270,7 @@ func init_route_handlers() { r.HandleFunc("/export", userHandler(exportHandler)) r.HandleFunc("/added", userHandler(addedHandler)) + r.HandleFunc("/categories", getPostHandler(userHandler(categoriesFormHandler), userHandler(categoriesPostHandler))) http.Handle("/", r) } diff --git a/templates.go b/templates.go new file mode 100644 index 0000000..1501575 --- /dev/null +++ b/templates.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "html/template" + "path/filepath" + "regexp" + "net/http" +) + +var ( + templates = map[string]*template.Template{} +) + +// Tempalte helper functions +var funcMap = template.FuncMap { + "add": func (x, y int) int { return x + y }, + "minus": func (x, y int) int { return x - y }, +} + + +func initTemplates() { + files, _ := filepath.Glob("templates/pages/*.html") + re := regexp.MustCompile("templates/pages/(.*).html") + fmt.Println("Loading Templates:") + for _, t := range files { + name := re.FindStringSubmatch(t) + fmt.Println(" ", name[1]) + templates[name[1]] = template.Must(template.New(name[1]).Funcs(funcMap).ParseFiles("templates/layout.html", t)) + } +} + +func ShowTemplate(template string, w http.ResponseWriter, data map[string]interface{}) { + err := templates[template].ExecuteTemplate(w, "layout.html", data) + if err != nil { + fmt.Println("Exec err: ", err) + } + // TODO: show error 500 page +} \ No newline at end of file diff --git a/templates/layout.html b/templates/layout.html index a4a5488..65f5bbe 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -27,6 +27,7 @@ transmet + categories