Convert templates to partials

This commit is contained in:
2021-12-02 15:00:53 +00:00
parent 67f7022b90
commit 77ae9d2dfd
16 changed files with 261 additions and 237 deletions

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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 {

View File

@@ -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) {

45
http/templates.go Normal file
View File

@@ -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,
}
}