From a158f158f2255c684d4021133b67306598786f92 Mon Sep 17 00:00:00 2001 From: Meutel Date: Sun, 30 Jul 2017 17:20:48 +0200 Subject: [PATCH] HTML to templates (test index/Series) --- bouquins/bouquins.go | 61 +++++++++++++++++++++----- main.go | 14 ++++-- {assets/html => templates}/author.html | 8 ++-- {assets/html => templates}/book.html | 20 ++++----- {assets/html => templates}/index.html | 40 ++++++++++------- {assets/html => templates}/series.html | 8 ++-- 6 files changed, 103 insertions(+), 48 deletions(-) rename {assets/html => templates}/author.html (91%) rename {assets/html => templates}/book.html (84%) rename {assets/html => templates}/index.html (79%) rename {assets/html => templates}/series.html (90%) diff --git a/bouquins/bouquins.go b/bouquins/bouquins.go index aee20cf..a2aaa94 100644 --- a/bouquins/bouquins.go +++ b/bouquins/bouquins.go @@ -1,8 +1,20 @@ package bouquins -import "net/http" +import ( + "html/template" + "log" + "net/http" +) + +const ( + TPL_BOOKS = "books.html" + TPL_AUTHORS = "authors.html" + TPL_SERIES = "series.html" + TPL_INDEX = "index.html" +) type Bouquins struct { + *template.Template } /* @@ -53,13 +65,13 @@ type BookData struct { */ type BookAdv struct { Book - Authors []Author + Authors []*Author Tags []string } type AuthorFull struct { Author - Books []BookAdv + Books []*BookAdv } type BookFull struct { @@ -78,24 +90,51 @@ type BookFull struct { type SeriesAdv struct { Series - Books int64 - Authors []Author + Count int64 + Authors []*Author } type SeriesFull struct { SeriesAdv - Bools []Book + Books []*Book } -func (*Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) { - http.Redirect(res, req, "/html/index.html", http.StatusSeeOther) +type IndexModel struct { + BooksCount int64 + Books []*BookAdv + Series []*SeriesAdv + Authors []*AuthorAdv } -func (*Bouquins) BooksPage(res http.ResponseWriter, req *http.Request) { + +func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) { + series := make([]*SeriesAdv, 0) + a1 := make([]*Author, 0) + s1 := &SeriesAdv{ + Series{ + 666, + "Serie 1", + }, + 12, + a1, + } + series = append(series, s1) + model := &IndexModel{ + 123, + nil, + series, + nil, + } + err := app.Template.ExecuteTemplate(res, TPL_INDEX, model) + if err != nil { + log.Print(err) + } +} +func (app *Bouquins) BooksPage(res http.ResponseWriter, req *http.Request) { panic("not implemented") } -func (*Bouquins) AuthorsPage(res http.ResponseWriter, req *http.Request) { +func (app *Bouquins) AuthorsPage(res http.ResponseWriter, req *http.Request) { panic("not implemented") } -func (*Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) { +func (app *Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) { panic("not implemented") } diff --git a/main.go b/main.go index ae2d5a4..dc5c061 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package main import ( + "html/template" + "log" "net/http" "meutel.net/meutel/go-bouquins/bouquins" @@ -11,19 +13,25 @@ const ( BOOKS = "/books" AUTHORS = "/authors" SERIES = "/series" - HTML = "/html/" JS = "/js/" CSS = "/css/" ) func init() { - app := new(bouquins.Bouquins) + tpl, err := template.ParseGlob("templates/*.html") + if err != nil { + log.Fatalln(err) + } + + app := &bouquins.Bouquins{ + tpl, + } + assets() router(app) } func assets() { - http.Handle(HTML, http.FileServer(http.Dir("assets"))) http.Handle(JS, http.FileServer(http.Dir("assets"))) http.Handle(CSS, http.FileServer(http.Dir("assets"))) } diff --git a/assets/html/author.html b/templates/author.html similarity index 91% rename from assets/html/author.html rename to templates/author.html index e7c881c..90a9264 100644 --- a/assets/html/author.html +++ b/templates/author.html @@ -29,7 +29,7 @@ @@ -42,7 +42,7 @@
@@ -51,7 +51,7 @@
@@ -60,7 +60,7 @@
diff --git a/assets/html/book.html b/templates/book.html similarity index 84% rename from assets/html/book.html rename to templates/book.html index 4e28a53..c454edc 100644 --- a/assets/html/book.html +++ b/templates/book.html @@ -34,14 +34,14 @@

- {{ book.title }} + {{ .Book.Title }}

@@ -50,11 +50,11 @@

- Auteur{{ book.authors.length > 1 ? 's' : '' }} + Auteur{{/* .Book.Authors.Length > 1 ? 's' : '' */}}

@@ -62,26 +62,26 @@ Serie
- {{ book.series.name }} - {{ book.series.idx }} + {{ .Book.Series.Name }} + {{ .Book.Series.Idx }}

Langue

- +

Tags

Détails

diff --git a/assets/html/index.html b/templates/index.html similarity index 79% rename from assets/html/index.html rename to templates/index.html index 4a52f97..83992f2 100644 --- a/assets/html/index.html +++ b/templates/index.html @@ -28,7 +28,7 @@

Bouquins

-

Cette bibliothèque contient actuellement {{ booksCount }} livres et BD en format papier ou électronique.

+

Cette bibliothèque contient actuellement {{ .BooksCount }} livres et BD en format papier ou électronique.

@@ -40,7 +40,8 @@ - + {{ if .Series }} +
+ {{ range .Series }} - + + {{ end }}
Nom @@ -50,20 +51,24 @@ Auteur(s)
- {{ serie.name }} + {{ .Name }} {{ serie.count }}{{ .Count }} - + {{ range .Authors }} + + {{ .Name }}  + {{ end }}
- + {{ end }} + {{ if .Authors }} +
- +
Nom @@ -74,12 +79,14 @@
- {{ author.name }} + {{ .Author.Name }} {{ author.count }}{{ .Author.Count }}
- + {{ end }} + {{ if .Books }} +
+ {{ .Book.Title }}
Nom @@ -90,22 +97,23 @@
- {{ book.title }}
+ {{ end }}