From 0891a94767cafc13307482cd0c4f76db3bcc3b5e Mon Sep 17 00:00:00 2001 From: jan Date: Mon, 28 Dec 2015 11:36:00 +0100 Subject: =?UTF-8?q?'html'=20template=20func=20zum=20rendern=20von=20HTML?= =?UTF-8?q?=20in=20templates.=20wird=20im=20dashboard=20verwendet=20f?= =?UTF-8?q?=C3=BCr=20mehr=20dynamik.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit diff --git a/frontend/renderer.go b/frontend/renderer.go index e9b9931..a3aa83d 100644 --- a/frontend/renderer.go +++ b/frontend/renderer.go @@ -7,8 +7,9 @@ import ( // Action ist die Aktion auf einer Card. type Action struct { - Name string - Link string + Name string + Link string + Disabled bool } // Card ist eine Karte im Materialize-Design. @@ -41,8 +42,14 @@ func (r *Renderer) SetDefaultData(data map[string]interface{}) { // New erstellt einen neuen Renderer und sucht alle Templates aus dem gegebenen pfad. func New(path string) *Renderer { + // Custom Template Funcs + funcMap := template.FuncMap{ + "html": func(in string) template.HTML { + return template.HTML(in) + }, + } r := &Renderer{ - template.Must(template.ParseGlob(path + "/*")), + template.Must(template.ParseGlob(path + "/*")).Funcs(funcMap), make(map[string]interface{}), } diff --git a/modules/lists/lists.go b/modules/lists/lists.go index 154bf74..47501c2 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -143,32 +143,30 @@ func (m *Module) FromID(id int) (*List, error) { return lists[0], nil } -func mkCard(title, description string, actions ...frontend.Action) frontend.Card { - card := frontend.Card{ - Title: title, - Description: description, - Size: "medium", - } - - for _, action := range actions { - card.Actions = append(card.Actions, action) - } - - return card -} - -func rowsToCard(rows *sql.Rows) []frontend.Card { +func listsToCards(lists []*List) []frontend.Card { var cards []frontend.Card - for rows.Next() { - var id int - var title, description string - if err := rows.Scan(&id, &title, &description); err != nil { - log.Println("error scanning row", err) - continue + for _, list := range lists { + s := "s" + if len(list.Grils) == 1 { + s = "" } - - cards = append(cards, mkCard(title, description, frontend.Action{"anguckieren", fmt.Sprintf("/list/%d", id)})) + cards = append(cards, frontend.Card{ + Title: list.Name, + Description: list.Description, + Size: "medium", + Actions: []frontend.Action{ + frontend.Action{ + Name: "anguckieren", + Link: fmt.Sprintf("/list/%d", list.ID), + }, + frontend.Action{ + Name: fmt.Sprintf(`%d gril%s`, len(list.Grils), s), + Link: "#", + Disabled: true, + }, + }, + }) } return cards } @@ -178,16 +176,15 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar var categories []grilist.DashboardCategory // Neue Listen - rows, err := m.g.DB.Query(`SELECT id, name, description FROM grilist.lists ORDER BY id DESC LIMIT 5`) + lists, err := m.getLists(`1=1 ORDER BY id DESC LIMIT 5`) if err != nil { log.Println(err) return categories } - defer rows.Close() categories = append(categories, grilist.DashboardCategory{ Title: "Neueste Listen", - Cards: rowsToCard(rows), + Cards: listsToCards(lists), }) if user == nil { @@ -195,16 +192,15 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar } // Listen des Benutzers - rows, err = m.g.DB.Query(`SELECT id, name, description FROM grilist.lists WHERE user_id = $1`, user.ID) + lists, err = m.getLists(`user_id = $1`, user.ID) if err != nil { log.Println(err) - return []grilist.DashboardCategory{} + return categories } - defer rows.Close() categories = append(categories, grilist.DashboardCategory{ Title: "Meine Listen", - Cards: rowsToCard(rows), + Cards: listsToCards(lists), }) return categories diff --git a/views/dashboard.html b/views/dashboard.html index f2d410d..0551285 100644 --- a/views/dashboard.html +++ b/views/dashboard.html @@ -16,12 +16,16 @@
- {{ $card.Title }} -

{{ $card.Description }}

+ {{ $card.Title | html }} +

{{ $card.Description | html }}

{{ range $action := $card.Actions }} - {{ $action.Name }} + {{ if $action.Disabled }} + {{ $action.Name | html }} + {{ else }} + {{ $action.Name | html }} + {{ end }} {{ end }}
-- cgit v0.10.1