From 6438250ef7c44d1d811097fc851e5285a89b28d3 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Sun, 23 Aug 2015 11:04:14 -0700 Subject: [PATCH] Work on news list page TODO: - fix category path printer (only prints last if there is a parent) - dates are all NOW --- css/template.css | 8 ++++++++ route_handlers.go | 4 ++-- templates.go | 24 ++++++++++++++++++++++++ templates/pages/news.html | 23 ++++++++++++++++++++--- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/css/template.css b/css/template.css index 3133551..28f02e9 100644 --- a/css/template.css +++ b/css/template.css @@ -9,3 +9,11 @@ body { .col-form .row .col-xs-2 { text-align: right; } + +.news-posts h3 { + display: inline; +} + +.news-row { + margin-bottom: 1em; +} \ No newline at end of file diff --git a/route_handlers.go b/route_handlers.go index c0a0dc8..e89d1e6 100644 --- a/route_handlers.go +++ b/route_handlers.go @@ -337,7 +337,7 @@ func newsFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, se session.AddFlash("Error loading news", flash_err) } - ShowTemplate("news", w, map[string]interface{}{"user": user, "flashes": flashes, "news": news, "count": count}) + ShowTemplate("news", w, map[string]interface{}{"user": user, "flashes": flashes, "news": news, "count": count, "categories": categories.CategoriesFlat}) } @@ -358,7 +358,7 @@ func init_route_handlers() { r.HandleFunc("/logout", userHandler(LogoutHandler)) r.HandleFunc("/add", getPostHandler(userHandler(addFormHandler), userHandler(addPostHandler))) - r.HandleFunc("/", userHandler(templateFormHandler)) + r.HandleFunc("/", userHandler(newsFormHandler)) r.HandleFunc("/news", userHandler(newsFormHandler)) r.HandleFunc("/export", userHandler(templateFormHandler)) r.HandleFunc("/export-commit", userHandler(exportHandler)) diff --git a/templates.go b/templates.go index 1c85c54..80ec0d8 100644 --- a/templates.go +++ b/templates.go @@ -7,6 +7,9 @@ import ( "regexp" "net/http" "errors" + "time" + "strings" + "github.com/dballard/transmet/categories" ) var ( @@ -29,6 +32,8 @@ func dict (values ...interface{}) (map[string]interface{}, error) { return dict, nil } +// string multiplication +// stringTimes(3, "Foo") => "FooFooFoo" func stringTimes(times int, str string) string { result := "" for i := 0; i < times; i ++ { @@ -37,12 +42,31 @@ func stringTimes(times int, str string) string { return result } +// Turns a Time into a formated string +func dateFormat(t time.Time) string { + return t.Format(time.ANSIC) +} + +// takes a category_id and returns "Root / Parent / Category" +func fullCategoryPath(categoriesFlat map[int]*categories.Category, category_id int) string { + fmt.Println("fullCategoryPath: ", category_id) + var categoryNames []string = nil + var category *categories.Category = categoriesFlat[category_id] + for ; category.Parent.Valid; category = categoriesFlat[int(category.Parent.Int64)] { + fmt.Println("fullCategoryPath LOOP: ", category.Name) + categoryNames = append(categoryNames, category.Name) + } + return strings.Join(categoryNames, " / ") +} + // 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, + "dateFormat": dateFormat, + "fullCategoryPath": fullCategoryPath, } diff --git a/templates/pages/news.html b/templates/pages/news.html index 15e48e4..37e768e 100644 --- a/templates/pages/news.html +++ b/templates/pages/news.html @@ -1,14 +1,31 @@ {{define "body"}}

News

{{template "flashes" .}} +
{{range $news_post := .news}} - {{template "row-news" $news_post}} + {{template "row-news" dict "post" $news_post "categories" $.categories}} {{end}} +
{{end}} {{define "row-news"}} -

-- {{.Title}}

- +
+
+
+

{{.post.Title}}

+
+
+ {{dateFormat .post.Date}} +
+ +
+ Category: {{fullCategoryPath .categories .post.Category_id}} +
+
+
{{.post.Notes}}
+
{{end}}