From 6fabf512767b458a44b4b2547a7b07e9db71c00a Mon Sep 17 00:00:00 2001 From: jan Date: Sat, 26 Dec 2015 11:41:42 +0100 Subject: =?UTF-8?q?default=20data=20im=20renderer,=20damit=20wir=20sp?= =?UTF-8?q?=C3=A4ter=20krasse=20sachen=20machen=20k=C3=B6nnen.=20es=20gibt?= =?UTF-8?q?=20jetzt=20eine=20globale=20navbar,=20damit=20man=20nicht=20meh?= =?UTF-8?q?r=20den=20zur=C3=BCckknopf=20dr=C3=BCcken=20muss,=20um=20bis=20?= =?UTF-8?q?zur=20startseite=20zu=20kommen.=20ich=20habe=20einen=20gro?= =?UTF-8?q?=C3=9Fen=20D.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit diff --git a/dashboard.go b/dashboard.go index 6e81d6b..63dcdfc 100644 --- a/dashboard.go +++ b/dashboard.go @@ -25,5 +25,6 @@ func viewDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) data := make(map[string]interface{}) data["categories"] = categories + data["user"] = user app.Renderer.RenderPage("dashboard", w, data) } diff --git a/frontend/renderer.go b/frontend/renderer.go index 0a2b7e9..e9b9931 100644 --- a/frontend/renderer.go +++ b/frontend/renderer.go @@ -21,13 +21,29 @@ type Card struct { // Renderer rendert Seiten. type Renderer struct { - templates *template.Template + templates *template.Template + defaultData map[string]interface{} +} + +// DefaultData gibt die Standard-Daten für die Views zurück. Das ist immer eine Kopie weil Reasons. +func (r *Renderer) DefaultData() map[string]interface{} { + d := make(map[string]interface{}) + for k, v := range r.defaultData { + d[k] = v + } + return d +} + +// SetDefaultData updated die standarddaten +func (r *Renderer) SetDefaultData(data map[string]interface{}) { + r.defaultData = data } // New erstellt einen neuen Renderer und sucht alle Templates aus dem gegebenen pfad. func New(path string) *Renderer { r := &Renderer{ template.Must(template.ParseGlob(path + "/*")), + make(map[string]interface{}), } return r @@ -35,6 +51,12 @@ func New(path string) *Renderer { // RenderPage rendert eine bestimmte Seite (basierend auf dem Template-Namen) und den gegebenen Daten. func (r *Renderer) RenderPage(name string, w http.ResponseWriter, data map[string]interface{}) { + // default daten einspeisen + for k, v := range r.defaultData { + if _, ok := data[k]; !ok { + data[k] = v + } + } err := r.templates.ExecuteTemplate(w, name, data) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/main.go b/main.go index 70763d4..dacdfe1 100644 --- a/main.go +++ b/main.go @@ -9,8 +9,8 @@ import ( "fagott.pw/charakterin" "fagott.pw/grilist/frontend" "fagott.pw/grilist/grilist" - "fagott.pw/grilist/modules/lists" "fagott.pw/grilist/modules/grils" + "fagott.pw/grilist/modules/lists" "github.com/julienschmidt/httprouter" _ "github.com/lib/pq" diff --git a/modules/grils/grils.go b/modules/grils/grils.go index fbe57e3..3663c7f 100644 --- a/modules/grils/grils.go +++ b/modules/grils/grils.go @@ -73,10 +73,7 @@ func (m *GrilsModule) FromID(id int) (*Gril, error) { } func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { - loggedIn := false - if user, _ := m.g.Charakterin.GetUserFromRequest(r); user != nil { - loggedIn = true - } + user, _ := m.g.Charakterin.GetUserFromRequest(r) sid := p.ByName("id") id, err := strconv.Atoi(sid) @@ -91,8 +88,8 @@ func (m *GrilsModule) viewGril(w http.ResponseWriter, r *http.Request, p httprou return } - data := make(map[string]interface{}) - data["loggedIn"] = loggedIn + data := m.g.Renderer.DefaultData() + data["user"] = user data["gril"] = gril m.g.Renderer.RenderPage("gril", w, data) diff --git a/modules/lists/lists.go b/modules/lists/lists.go index 519bb36..970b15b 100644 --- a/modules/lists/lists.go +++ b/modules/lists/lists.go @@ -207,10 +207,7 @@ func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.Dashboar } func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.Params) { - loggedIn := false - if user, _ := m.g.Charakterin.GetUserFromRequest(r); user != nil { - loggedIn = true - } + user, _ := m.g.Charakterin.GetUserFromRequest(r) sid := p.ByName("id") id, err := strconv.Atoi(sid) @@ -225,8 +222,8 @@ func (m *Module) viewList(w http.ResponseWriter, r *http.Request, p httprouter.P return } - data := make(map[string]interface{}) - data["loggedIn"] = loggedIn + data := m.g.Renderer.DefaultData() + data["user"] = user data["list"] = list m.g.Renderer.RenderPage("list", w, data) diff --git a/views/dashboard.html b/views/dashboard.html index a000b57..a9a01d9 100644 --- a/views/dashboard.html +++ b/views/dashboard.html @@ -6,10 +6,8 @@ + {{ template "navbar" . }}
-
- Ausloggen -
{{ range $category := .categories }}

{{ $category.Title }}

diff --git a/views/gril.html b/views/gril.html index d62fc75..d623abf 100644 --- a/views/gril.html +++ b/views/gril.html @@ -8,12 +8,8 @@ + {{ template "navbar" . }}
- {{ if .loggedIn }} -
- Ausloggen -
- {{ end }}

{{ $gril.RomajiName }}{{ $gril.KanjiName }}

diff --git a/views/index.html b/views/index.html deleted file mode 100644 index 81c801f..0000000 --- a/views/index.html +++ /dev/null @@ -1,34 +0,0 @@ -{{ define "index" }} - - - {{ template "materialize" }} - grilist - - - - -
-
-

grilist#tamtam

- - Ausloggen -
-
- - -{{ end }} \ No newline at end of file diff --git a/views/list.html b/views/list.html index 63cfd9c..fb5f7a3 100644 --- a/views/list.html +++ b/views/list.html @@ -8,12 +8,8 @@ + {{ template "navbar" . }}
- {{ if .loggedIn }} -
- Ausloggen -
- {{ end }}

{{ $list.Name }}von xyz


    {{ range $lg := $list.Grils }} diff --git a/views/navbar.html b/views/navbar.html new file mode 100644 index 0000000..c4bbace --- /dev/null +++ b/views/navbar.html @@ -0,0 +1,13 @@ +{{ define "navbar" }} + +{{ end }} \ No newline at end of file -- cgit v0.10.1