From 2598dd481bd70a1bf3a137b67c40f9d80028c208 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Sat, 23 May 2015 10:49:27 -0700 Subject: [PATCH] categories list page: show parents --- templates.go | 27 +++++++++++++++++ templates/pages/categories.html | 52 +++++++++++++++++---------------- 2 files changed, 54 insertions(+), 25 deletions(-) diff --git a/templates.go b/templates.go index 1501575..1c85c54 100644 --- a/templates.go +++ b/templates.go @@ -6,16 +6,43 @@ import ( "path/filepath" "regexp" "net/http" + "errors" ) var ( templates = map[string]*template.Template{} ) +// template helper function +func dict (values ...interface{}) (map[string]interface{}, error) { + if len(values)%2 != 0 { + return nil, errors.New("invalid dict call") + } + dict := make(map[string]interface{}, len(values)/2) + for i := 0; i < len(values); i+=2 { + key, ok := values[i].(string) + if !ok { + return nil, errors.New("dict keys must be strings") + } + dict[key] = values[i+1] + } + return dict, nil +} + +func stringTimes(times int, str string) string { + result := "" + for i := 0; i < times; i ++ { + result += str + } + return result +} + // 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 }, + "dict": dict, + "stringTimes": stringTimes, } diff --git a/templates/pages/categories.html b/templates/pages/categories.html index 7c2a1f1..e9d6d6d 100644 --- a/templates/pages/categories.html +++ b/templates/pages/categories.html @@ -1,48 +1,50 @@ {{define "body"}}

Categories

{{template "flashes" .}} -
+ {{range $category := .categories}} - {{template "row-category" $category}} + {{template "row-category" dict "category" $category "categories" $.categories}} {{end}} -
{{end}} {{define "row-category"}} - {{if .Parent.Valid }} - {{with .Depth}} -
-
- {{end}} - {{else}} -
- {{end}} - - {{.Name}} delete +
- + {{range $category := .categories}} - {{template "option-category" $category }} + {{if $.category.Parent.Valid }} + {{template "option-category" dict "category" $category "id" $.category.Parent.Value}} + {{else}} + {{template "option-category" dict "category" $category "id" -1}} + {{end}} {{end}} - +
- - {{range $child := .Children}} - {{template "row-category" $child}} + {{range $child := .category.Children}} + {{template "row-category" dict "category" $child "categories" $.categories}} {{end}} {{end}} {{define "option-category"}} - - {{range $child := .Children}} - {{template "option-category" $child}} + + {{range $child := .category.Children}} + {{template "option-category" dict "category" $child "id" $.id}} {{end}} -======= ->>>>>>> fea21c9d10d89f90b71988dcb41722527b947f60 {{end}} \ No newline at end of file