diff --git a/http/accounts.go b/http/accounts.go index c823a93..fa1b497 100644 --- a/http/accounts.go +++ b/http/accounts.go @@ -39,5 +39,5 @@ func (h *Handler) accounts(c *gin.Context) { Accounts: accounts, } - c.HTML(http.StatusOK, "accounts", d) + c.HTML(http.StatusOK, "accounts.html", d) } diff --git a/http/budget.go b/http/budget.go index d0731b1..1958478 100644 --- a/http/budget.go +++ b/http/budget.go @@ -48,5 +48,5 @@ func (h *Handler) budget(c *gin.Context) { Transactions: transactions, } - c.HTML(http.StatusOK, "budget", d) + c.HTML(http.StatusOK, "budget.html", d) } diff --git a/http/dashboard.go b/http/dashboard.go index 06028fe..52aea3d 100644 --- a/http/dashboard.go +++ b/http/dashboard.go @@ -25,7 +25,7 @@ func (h *Handler) dashboard(c *gin.Context) { Token: token, Budgets: budgets, } - c.HTML(http.StatusOK, "dashboard", d) + c.HTML(http.StatusOK, "dashboard.html", d) } type DashboardData struct { diff --git a/http/http.go b/http/http.go index 4411b53..471543f 100644 --- a/http/http.go +++ b/http/http.go @@ -6,8 +6,6 @@ import ( "net/http" "time" - "html/template" - "git.javil.eu/jacob1123/budgeteer" "git.javil.eu/jacob1123/budgeteer/bcrypt" "git.javil.eu/jacob1123/budgeteer/postgres" @@ -34,8 +32,12 @@ const ( func (h *Handler) Serve() { router := gin.Default() - templ := template.Must(template.New("").Funcs(router.FuncMap).ParseFS(web.Templates, "*")) - router.SetHTMLTemplate(templ) + templates, err := NewTemplates(router.FuncMap) + if err != nil { + panic(err) + } + + router.HTMLRender = templates static, err := fs.Sub(web.Static, "static") if err != nil { @@ -43,7 +45,7 @@ func (h *Handler) Serve() { } router.StaticFS("/static", http.FS(static)) - router.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "index", nil) }) + router.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "index.html", nil) }) router.GET("/login", h.login) router.GET("/register", h.register) authenticatedFrontend := router.Group("") @@ -180,7 +182,7 @@ func (h *Handler) login(c *gin.Context) { return } - c.HTML(http.StatusOK, "login", nil) + c.HTML(http.StatusOK, "login.html", nil) } func (h *Handler) register(c *gin.Context) { @@ -189,7 +191,7 @@ func (h *Handler) register(c *gin.Context) { return } - c.HTML(http.StatusOK, "register", nil) + c.HTML(http.StatusOK, "register.html", nil) } func logout(c *gin.Context) { diff --git a/http/templates.go b/http/templates.go new file mode 100644 index 0000000..2d03b07 --- /dev/null +++ b/http/templates.go @@ -0,0 +1,45 @@ +package http + +import ( + "fmt" + "html/template" + "io/fs" + + "git.javil.eu/jacob1123/budgeteer/web" + "github.com/gin-gonic/gin/render" +) + +type Templates struct { + templates map[string]*template.Template +} + +func NewTemplates(funcMap template.FuncMap) (*Templates, error) { + templates, err := fs.Glob(web.Templates, "*.tpl") + if err != nil { + return nil, err + } + + result := &Templates{ + templates: make(map[string]*template.Template, 0), + } + pages, err := fs.Glob(web.Templates, "*.html") + for _, page := range pages { + allTemplates := append([]string{page}, templates...) + tpl, err := template.New(page).Funcs(funcMap).ParseFS(web.Templates, allTemplates...) + fmt.Printf("page: %s, templates: %v\n", page, templates) + if err != nil { + return nil, err + } + result.templates[page] = tpl + } + + return result, nil +} + +func (tpl *Templates) Instance(name string, obj interface{}) render.Render { + return render.HTML{ + Template: tpl.templates[name], + Name: name, + Data: obj, + } +} diff --git a/web/accounts.html b/web/accounts.html index f55dc1e..6cb4219 100644 --- a/web/accounts.html +++ b/web/accounts.html @@ -1,23 +1,17 @@ -{{define "accounts"}} - - - - Budgets +{{define "title"}} + Accounts +{{end}} - {{template "head"}} - - - -
- {{range .Accounts}} -
- {{.Name}} - -
- {{end}} -
- - +{{define "new"}} +{{end}} + +{{template "base" .}} + +{{define "main"}} + {{range .Accounts}} +
+ {{.Name}} + +
+ {{end}} {{end}} \ No newline at end of file diff --git a/web/base.tpl b/web/base.tpl new file mode 100644 index 0000000..b63c812 --- /dev/null +++ b/web/base.tpl @@ -0,0 +1,31 @@ +{{define "base"}} + + + + + + + + + + + + + + + + {{template "title" .}} - Budgeteer + + {{block "more-head" .}}{{end}} + + + +
+ {{template "main" .}} +
+ {{block "new" .}}{{end}} + + +{{end}} \ No newline at end of file diff --git a/web/budget-new.html b/web/budget-new.tpl similarity index 100% rename from web/budget-new.html rename to web/budget-new.tpl diff --git a/web/budget.html b/web/budget.html index 1aaa45b..54b26b2 100644 --- a/web/budget.html +++ b/web/budget.html @@ -1,32 +1,26 @@ -{{define "budget"}} - - - - Budgets +{{template "base" .}} - {{template "head"}} - - - -
Go to Accounts
-
- New Budget - -
- - {{range .Transactions}} - - - - - - {{end}} -
{{.Date}} - {{.Memo.String}} - {{.Amount}}
- {{template "transaction-new"}} - - +{{define "title"}}Budget{{end}} + +{{define "new"}} + {{template "transaction-new"}} +{{end}} + +{{define "main"}} +
Go to Accounts
+
+ New Budget + +
+ + {{range .Transactions}} + + + + + + {{end}} +
{{.Date}} + {{.Memo.String}} + {{.Amount}}
{{end}} \ No newline at end of file diff --git a/web/dashboard.html b/web/dashboard.html index 33c7c44..d34b14c 100644 --- a/web/dashboard.html +++ b/web/dashboard.html @@ -1,28 +1,22 @@ -{{define "dashboard"}} - - - - Budgets +{{define "title"}} + Budgets +{{end}} - {{template "head"}} - - - -
- {{range .Budgets}} -
- {{.Name}} - -
- {{end}} -
- New Budget - -
-
- {{template "budget-new"}} - - +{{define "new"}} + {{template "budget-new"}} +{{end}} + +{{template "base" .}} + +{{define "main"}} + {{range .Budgets}} +
+ {{.Name}} + +
+ {{end}} +
+ New Budget + +
{{end}} \ No newline at end of file diff --git a/web/head.html b/web/head.html deleted file mode 100644 index 7dd4832..0000000 --- a/web/head.html +++ /dev/null @@ -1,13 +0,0 @@ -{{define "head"}} - - - - - - - - - - - -{{end}} \ No newline at end of file diff --git a/web/index.html b/web/index.html index 9b11d49..554ec5e 100644 --- a/web/index.html +++ b/web/index.html @@ -1,21 +1,17 @@ -{{define "index"}} - - - - Budgeteer +{{define "title"}} + Start +{{end}} - {{template "head"}} - - - -
- Willkommen bei Budgeteer, der neuen App für's Budget! -
-
- Login -
- - +{{define "new"}} +{{end}} + +{{template "base" .}} + +{{define "main"}} +
+ Willkommen bei Budgeteer, der neuen App für's Budget! +
+
+ Login or register +
{{end}} \ No newline at end of file diff --git a/web/login.html b/web/login.html index 9bc9609..38417d4 100644 --- a/web/login.html +++ b/web/login.html @@ -1,47 +1,38 @@ -{{define "login"}} - - - - Login +{{template "base" .}} - {{template "head"}} - - - - - -
-
-
- - -
+{{define "title"}}Login{{end}} -
- - -

- The entered credentials are invalid -

-
- - -
-
- - +{{define "more-head"}} + +{{end}} + +{{define "main"}} +
+
+ + +
+ +
+ + +

+ The entered credentials are invalid +

+
+ + +
{{end}} diff --git a/web/register.html b/web/register.html index acc3f77..d3c21f3 100644 --- a/web/register.html +++ b/web/register.html @@ -1,78 +1,68 @@ -{{define "register"}} - - - - Registration +{{define "title"}}Register{{end}} - {{template "head"}} +{{template "base" .}} + +{{define "more-head"}} + +{{end}} + +{{define "main"}} +
+
+ + +
+ +
+ + +
+ +
+ + + +
- - - - -
- -
- - -
- -
- - -
- -
- - - -
- -
- -
- Username already exists -
- - - -
- - + + {{end}} \ No newline at end of file diff --git a/web/templates.go b/web/templates.go index 67db5e9..9e52a20 100644 --- a/web/templates.go +++ b/web/templates.go @@ -2,7 +2,7 @@ package web import "embed" -//go:embed *.html +//go:embed *.html *.tpl var Templates embed.FS //go:embed static diff --git a/web/transaction-new.html b/web/transaction-new.tpl similarity index 100% rename from web/transaction-new.html rename to web/transaction-new.tpl