Factorise templates

This commit is contained in:
Meutel 2017-07-30 18:09:27 +02:00
parent 8a06369839
commit 8df3f87a23
8 changed files with 293 additions and 369 deletions

View File

@ -99,42 +99,55 @@ type SeriesFull struct {
Books []*Book Books []*Book
} }
type BouquinsModel struct {
Title string
}
// Constructor BouquinsModel
func NewBouquinsModel(title string) *BouquinsModel {
return &BouquinsModel{
title,
}
}
type IndexModel struct { type IndexModel struct {
BouquinsModel
BooksCount int64 BooksCount int64
Books []*BookAdv Books []*BookAdv
Series []*SeriesAdv Series []*SeriesAdv
Authors []*AuthorAdv Authors []*AuthorAdv
} }
func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) { // Constructor IndexModel
series := make([]*SeriesAdv, 0) func NewIndexModel(title string, count int64) *IndexModel {
a1 := make([]*Author, 0) return &IndexModel{
s1 := &SeriesAdv{ *NewBouquinsModel(title),
Series{ count,
666, nil,
"Serie 1",
},
12,
a1,
}
series = append(series, s1)
model := &IndexModel{
123,
nil, nil,
series,
nil, nil,
} }
err := app.Template.ExecuteTemplate(res, TPL_INDEX, model) }
// ROUTES //
func (app *Bouquins) render(res http.ResponseWriter, tpl string, model interface{}) {
err := app.Template.ExecuteTemplate(res, tpl, model)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
} }
func (app *Bouquins) IndexPage(res http.ResponseWriter, req *http.Request) {
model := NewIndexModel("", 123)
app.render(res, TPL_INDEX, model)
}
func (app *Bouquins) BooksPage(res http.ResponseWriter, req *http.Request) { func (app *Bouquins) BooksPage(res http.ResponseWriter, req *http.Request) {
panic("not implemented") app.render(res, TPL_BOOKS, nil)
} }
func (app *Bouquins) AuthorsPage(res http.ResponseWriter, req *http.Request) { func (app *Bouquins) AuthorsPage(res http.ResponseWriter, req *http.Request) {
panic("not implemented") app.render(res, TPL_AUTHORS, nil)
} }
func (app *Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) { func (app *Bouquins) SeriesPage(res http.ResponseWriter, req *http.Request) {
panic("not implemented") app.render(res, TPL_SERIES, nil)
} }

View File

@ -15,6 +15,7 @@ const (
SERIES = "/series" SERIES = "/series"
JS = "/js/" JS = "/js/"
CSS = "/css/" CSS = "/css/"
FONTS = "/fonts/"
) )
func init() { func init() {
@ -22,11 +23,9 @@ func init() {
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
app := &bouquins.Bouquins{ app := &bouquins.Bouquins{
tpl, tpl,
} }
assets() assets()
router(app) router(app)
} }
@ -34,6 +33,7 @@ func init() {
func assets() { func assets() {
http.Handle(JS, http.FileServer(http.Dir("assets"))) http.Handle(JS, http.FileServer(http.Dir("assets")))
http.Handle(CSS, http.FileServer(http.Dir("assets"))) http.Handle(CSS, http.FileServer(http.Dir("assets")))
http.Handle(FONTS, http.FileServer(http.Dir("assets")))
} }
func router(app *bouquins.Bouquins) { func router(app *bouquins.Bouquins) {

View File

@ -1,31 +1,5 @@
<!DOCTYPE html> {{ template "header.html" . }}
<html lang="fr"> <div class="container" id="app">
<head>
<title>Auteur | Bouquins</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="utf-8" />
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="preload" href="/js/author.min.js" as="script">
<link rel="preload" href="/js/vue.min.js" as="script">
<link rel="prefetch" href="/js/author.min.js">
<link rel="prefetch" href="/js/vue.min.js">
</head>
<body>
<nav class="navbar navbar-inverse" id="nav">
<div class="container">
<ul class="nav navbar-nav">
<li><a href="index.html">Accueil</a></li>
<li><a href="search.html">Recherche</a></li>
<li><a href="#">A propos</a></li>
</ul>
<form class="navbar-form navbar-right" role="search" method="get" action="search.html">
<div class="form-group">
<input name="q" type="text" class="form-control" placeholder="Recherche">
</div>
</form>
</div>
</nav>
<div class="container" id="app">
<div class="page-header" v-if="author.id"> <div class="page-header" v-if="author.id">
<h1> <h1>
<span class="glyphicon glyphicon-user"></span> <span class="glyphicon glyphicon-user"></span>
@ -65,9 +39,5 @@
</ul> </ul>
</div> </div>
</div> </div>
</div> </div>
<script src="js/vue.min.js"></script> {{ template "footer.html" . }}
<script src="js/author.min.js"></script>
</body>
</html>

View File

@ -1,31 +1,5 @@
<!DOCTYPE html> {{ template "header.html" . }}
<html lang="fr"> <div class="container" id="app">
<head>
<title>Livre | Bouquins</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="utf-8" />
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="preload" href="/js/book.min.js" as="script">
<link rel="preload" href="/js/vue.min.js" as="script">
<link rel="prefetch" href="/js/book.min.js">
<link rel="prefetch" href="/js/vue.min.js">
</head>
<body>
<nav class="navbar navbar-inverse" id="nav">
<div class="container">
<ul class="nav navbar-nav">
<li><a href="index.html">Accueil</a></li>
<li><a href="search.html">Recherche</a></li>
<li><a href="#">A propos</a></li>
</ul>
<form class="navbar-form navbar-right" role="search" method="get" action="search.html">
<div class="form-group">
<input name="q" type="text" class="form-control" placeholder="Recherche">
</div>
</form>
</div>
</nav>
<div class="container" id="app">
<div class="page-header" v-if="book.id"> <div class="page-header" v-if="book.id">
<div class="row" v-if="book.has_cover"> <div class="row" v-if="book.has_cover">
<img :src="bookCover(book)" alt="Pas de couverture" title="Couverture" class="img-responsive img-rounded"/> <img :src="bookCover(book)" alt="Pas de couverture" title="Couverture" class="img-responsive img-rounded"/>
@ -84,9 +58,5 @@
<li v-if="book.publisher"><strong>Editeur</strong> {{ .Book.Publisher }}</li> <li v-if="book.publisher"><strong>Editeur</strong> {{ .Book.Publisher }}</li>
</ul> </ul>
</div> </div>
<!-- TODO cover --> </div>
</div> {{ template "footer.html" . }}
<script src="js/vue.min.js"></script>
<script src="js/book.min.js"></script>
</body>
</html>

3
templates/footer.html Normal file
View File

@ -0,0 +1,3 @@
<script src="js/vue.min.js"></script>
</body>
</html>

23
templates/header.html Normal file
View File

@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<title>Bouquins{{ if .Title }} - {{ .Title }}{{ end }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="utf-8" />
<link rel="stylesheet" href="/css/bootstrap.min.css">
</head>
<body>
<nav class="navbar navbar-inverse" id="nav">
<div class="container">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Accueil</a></li>
<li><a href="search.html">Recherche</a></li>
<li><a href="#">A propos</a></li>
</ul>
<form class="navbar-form navbar-right" role="search" method="get" action="search.html">
<div class="form-group">
<input name="q" type="text" class="form-control" placeholder="Recherche">
</div>
</form>
</div>
</nav>

View File

@ -1,31 +1,5 @@
<!DOCTYPE html> {{ template "header.html" . }}
<html lang="fr"> <div class="container" id="app">
<head>
<title>Bouquins</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="utf-8" />
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="preload" href="/js/index.min.js" as="script">
<link rel="preload" href="/js/vue.min.js" as="script">
<link rel="prefetch" href="/js/index.min.js">
<link rel="prefetch" href="/js/vue.min.js">
</head>
<body>
<nav class="navbar navbar-inverse" id="nav">
<div class="container">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Accueil</a></li>
<li><a href="search.html">Recherche</a></li>
<li><a href="#">A propos</a></li>
</ul>
<form class="navbar-form navbar-right" role="search" method="get" action="search.html">
<div class="form-group">
<input name="q" type="text" class="form-control" placeholder="Recherche">
</div>
</form>
</div>
</nav>
<div class="container" id="app">
<div class="jumbotron"> <div class="jumbotron">
<h1>Bouquins</h1> <h1>Bouquins</h1>
<p>Cette bibliothèque contient actuellement <strong>{{ .BooksCount }}</strong> livres et BD en format papier ou électronique.</p> <p>Cette bibliothèque contient actuellement <strong>{{ .BooksCount }}</strong> livres et BD en format papier ou électronique.</p>
@ -33,13 +7,15 @@
<button class="btn btn-primary" type="button" @click="showAuthors">Auteurs</button> <button class="btn btn-primary" type="button" @click="showAuthors">Auteurs</button>
<button class="btn btn-primary" type="button" @click="showSeries">Series</button> <button class="btn btn-primary" type="button" @click="showSeries">Series</button>
</div> </div>
<div class="table-responsive" v-if="books.length > 0 || authors.length > 0 || series.length > 0"> <div class="table-responsive">
{{ if (ne (len .Books) 0) or (ne (len .Authors) 0) or (ne (len .Series) 0) }}
<nav aria-label="Pages"> <nav aria-label="Pages">
<ul class="pager"> <ul class="pager">
<li class="previous" v-bind:class="{ disabled: page <= 1 }"><a href="#" @click="prevPage"><span aria-hidden="true">&larr;</span> Précédents</a></li> <li class="previous" v-bind:class="{ disabled: page <= 1 }"><a href="#" @click="prevPage"><span aria-hidden="true">&larr;</span> Précédents</a></li>
<li class="next"><a href="#" @click="nextPage">Suivants <span aria-hidden="true">&rarr;</span></a></li> <li class="next"><a href="#" @click="nextPage">Suivants <span aria-hidden="true">&rarr;</span></a></li>
</ul> </ul>
</nav> </nav>
{{ end }}
{{ if .Series }} {{ if .Series }}
<table class="table table-striped"> <table class="table table-striped">
<tr> <tr>
@ -114,15 +90,14 @@
</tr> </tr>
</table> </table>
{{ end }} {{ end }}
{{ if (ne (len .Books) 0) or (ne (len .Authors) 0) or (ne (len .Series) 0) }}
<nav aria-label="Pages"> <nav aria-label="Pages">
<ul class="pager"> <ul class="pager">
<li class="previous" v-bind:class="{ disabled: page <= 1 }"><a href="#" @click="prevPage"><span aria-hidden="true">&larr;</span> Précédents</a></li> <li class="previous" v-bind:class="{ disabled: page <= 1 }"><a href="#" @click="prevPage"><span aria-hidden="true">&larr;</span> Précédents</a></li>
<li class="next"><a href="#" @click="nextPage">Suivants <span aria-hidden="true">&rarr;</span></a></li> <li class="next"><a href="#" @click="nextPage">Suivants <span aria-hidden="true">&rarr;</span></a></li>
</ul> </ul>
</nav> </nav>
{{ end }}
</div> </div>
</div> </div>
<script src="js/vue.min.js"></script> {{ template "footer.html" . }}
<script src="js/index.min.js"></script>
</body>
</html>

View File

@ -1,31 +1,5 @@
<!DOCTYPE html> {{ template "header.html" . }}
<html lang="fr"> <div class="container" id="app">
<head>
<title>Serie | Bouquins</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="utf-8" />
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="preload" href="/js/series.min.js" as="script">
<link rel="preload" href="/js/vue.min.js" as="script">
<link rel="prefetch" href="/js/series.min.js">
<link rel="prefetch" href="/js/vue.min.js">
</head>
<body>
<nav class="navbar navbar-inverse" id="nav">
<div class="container">
<ul class="nav navbar-nav">
<li><a href="index.html">Accueil</a></li>
<li><a href="search.html">Recherche</a></li>
<li><a href="#">A propos</a></li>
</ul>
<form class="navbar-form navbar-right" role="search" method="get" action="search.html">
<div class="form-group">
<input name="q" type="text" class="form-control" placeholder="Recherche">
</div>
</form>
</div>
</nav>
<div class="container" id="app">
<div class="page-header" v-if="series"> <div class="page-header" v-if="series">
<h1> <h1>
<span class="glyphicon glyphicon-list"></span> <span class="glyphicon glyphicon-list"></span>
@ -52,9 +26,5 @@
</li> </li>
</ul> </ul>
</template> </template>
</div> </div>
<script src="js/vue.min.js"></script> {{ template "footer.html" . }}
<script src="js/series.min.js"></script>
</body>
</html>