start categories listing page, in the process of reworking the template code a bit

This commit is contained in:
Dan Ballard 2015-05-21 18:34:14 -07:00
parent ff9c3e7f77
commit fea21c9d10
7 changed files with 83 additions and 29 deletions

View File

@ -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
}

20
main.go
View File

@ -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)

View File

@ -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)
}

39
templates.go Normal file
View File

@ -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
}

View File

@ -27,6 +27,7 @@
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">transmet</a>
<a class="navbar-brand" href="/categories">categories</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">

View File

@ -0,0 +1,27 @@
{{define "body"}}
<h2 class="form-categories-heading">Categories</h2>
{{template "flashes" .}}
<div class="row">
{{range $category := .categories}}
{{template "row-category" $category}}
{{end}}
</div>
{{end}}
{{define "row-category"}}
{{if .Parent.Valid }}
{{with .Depth}}
<div class="col-xs-{{.}} child-depth"></div>
<div class="col-xs-{{minus 12 .}} category-row">
{{end}}
{{else}}
<div class="col-xs-12 category-row">
{{end}}
{{.Name}}</div>
{{range $child := .Children}}
{{template "row-category" $child}}
{{end}}
{{end}}

View File

@ -1,5 +1,5 @@
{{define "body"}}
<h2 class="form-add-heading">List</h2>
<h2 class="list-heading">List</h2>
{{template "flashes" .}}
<div class="row">