From 0d373d19908926ee2b62a26d15135112c80529f3 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Mon, 28 Jul 2025 03:34:26 +0100 Subject: [PATCH] Getting rid of the templating subpackage, complete unecessary --- src/assets/htmx/expense.html | 143 +----------------- src/assets/htmx/expensePaymentsTable.html | 72 +++++++++ src/assets/htmx/expenseShares.html | 66 ++++++++ src/assets/htmx/expenses.html | 2 + src/assets/htmx/{panic.tpl => panic.html} | 0 .../htmx/{terminal.gotempl => terminal.html} | 10 +- src/main.go | 19 ++- src/pages/400.go | 4 +- src/pages/500.go | 4 +- src/pages/categories.go | 6 +- src/pages/expenses.go | 94 ++++-------- src/pages/overview.go | 2 +- src/pages/stores.go | 6 +- src/pages/types.go | 6 +- src/templating/templating.go | 16 +- 15 files changed, 219 insertions(+), 231 deletions(-) create mode 100644 src/assets/htmx/expensePaymentsTable.html create mode 100644 src/assets/htmx/expenseShares.html rename src/assets/htmx/{panic.tpl => panic.html} (100%) rename src/assets/htmx/{terminal.gotempl => terminal.html} (84%) diff --git a/src/assets/htmx/expense.html b/src/assets/htmx/expense.html index cf5fbb9..54d8919 100644 --- a/src/assets/htmx/expense.html +++ b/src/assets/htmx/expense.html @@ -1,3 +1,4 @@ +{{ define "expense" }}
@@ -58,144 +59,12 @@
SHARES - - - - - - - - - - - - - - - - - {{ range .expense.Shares }} - {{ $share := . }} - - - - - - - {{ end }} -
IDUSERSHARE
- - - - - - - -
+ {{ template "expenseShares" . }}
PAYMENTS - -
 

- - - - - - - - - - - - - - - - - - {{ range .expense.Payments }} - {{ $payment := . }} - - - - - - - {{ end }} -
IDUSERPAYED
- - - - - - - - -
+ {{ template "expensePayments" . }}

@@ -213,11 +82,6 @@ {{- if eq .method "put" }}UPDATE{{- end }} {{- if eq .method "post" }}CREATE{{- end }} {{ if eq .method "put" }} - - - - - +
 

+ + + + + + + + + + + + + + + + + + {{ range .expense.Payments }} + {{ $payment := . }} + + + + + + + {{ end }} +
IDUSERPAYED
+ + + + + + + + +
+{{ end }} diff --git a/src/assets/htmx/expenseShares.html b/src/assets/htmx/expenseShares.html new file mode 100644 index 0000000..1610a19 --- /dev/null +++ b/src/assets/htmx/expenseShares.html @@ -0,0 +1,66 @@ +{{ define "expenseShares" }} + + + + + + + + + + + + + + + + + {{ range .expense.Shares }} + {{ $share := . }} + + + + + + + {{ end }} +
IDUSERSHARE
+ + + + + + + +
+{{ end }} diff --git a/src/assets/htmx/expenses.html b/src/assets/htmx/expenses.html index b2aa39a..e11ead0 100644 --- a/src/assets/htmx/expenses.html +++ b/src/assets/htmx/expenses.html @@ -1,3 +1,4 @@ +{{ define "expenses" }} @@ -20,3 +21,4 @@ {{ end }}
ID
+{{ end }} diff --git a/src/assets/htmx/panic.tpl b/src/assets/htmx/panic.html similarity index 100% rename from src/assets/htmx/panic.tpl rename to src/assets/htmx/panic.html diff --git a/src/assets/htmx/terminal.gotempl b/src/assets/htmx/terminal.html similarity index 84% rename from src/assets/htmx/terminal.gotempl rename to src/assets/htmx/terminal.html index 1b872a3..5a97e5e 100644 --- a/src/assets/htmx/terminal.gotempl +++ b/src/assets/htmx/terminal.html @@ -47,7 +47,15 @@ {{ end }}
- {{ .content }} + {{ if eq .content "expense" }} + {{ template "expense" . }} + {{ else if eq .content "expenses" }} + {{ template "expenses" . }} + {{ else if eq .content "newExpense" }} + {{ template "expense" . }} + {{ else }} +
Unknown page
+ {{ end }}
diff --git a/src/main.go b/src/main.go index b851281..97c2652 100644 --- a/src/main.go +++ b/src/main.go @@ -3,10 +3,12 @@ package main import ( "expenses/config" "expenses/pages" + "expenses/templating" + "html/template" "log" "net/http" - "path/filepath" + fp "path/filepath" "github.com/gin-gonic/gin" ) @@ -15,16 +17,21 @@ import ( var ginMode = "debug" func main() { - gin.SetMode(ginMode) - config.SetConfig("./config.yaml") - cfg := config.GetInstance() + + gin.SetMode(ginMode) + templates := template.Must( + template.New(""). + Funcs(templating.TmplFuncMap). + ParseGlob(fp.Join(cfg.AssetsDir, "htmx/*.html")), + ) + router := gin.Default() + router.SetHTMLTemplate(templates) router.StaticFS("/assets", http.Dir(cfg.AssetsDir)) - router.StaticFile("/favicon.ico", filepath.Join(cfg.AssetsDir, "img/favicon.webp")) - router.LoadHTMLGlob(filepath.Join(cfg.AssetsDir, "htmx/*.gotempl")) + router.StaticFile("/favicon.ico", fp.Join(cfg.AssetsDir, "img/favicon.webp")) pages.RouteHome(router) pages.RouteExpenses(router) diff --git a/src/pages/400.go b/src/pages/400.go index 6ac74b7..0be17d9 100644 --- a/src/pages/400.go +++ b/src/pages/400.go @@ -13,12 +13,12 @@ func NotFoundView(c *gin.Context, msg string) { cfg := config.GetInstance() errHtml := templating.HtmlTemplate( - cfg.AssetsDir+"htmx/panic.tpl", map[string]any{ + cfg.AssetsDir+"htmx/panic.html", map[string]any{ "msg": msg, }, ) - c.HTML(404, "terminal.gotempl", gin.H{ + c.HTML(404, "terminal.html", gin.H{ "warning": true, "renderNavBar": false, "content": errHtml, diff --git a/src/pages/500.go b/src/pages/500.go index b2ab958..3135b0b 100644 --- a/src/pages/500.go +++ b/src/pages/500.go @@ -11,12 +11,12 @@ func ServerErrorView(c *gin.Context, msg string) { cfg := config.GetInstance() errHtml := templating.HtmlTemplate( - cfg.AssetsDir+"htmx/panic.tpl", map[string]any{ + cfg.AssetsDir+"htmx/panic.html", map[string]any{ "msg": msg, }, ) - c.HTML(500, "terminal.gotempl", gin.H{ + c.HTML(500, "terminal.html", gin.H{ "warning": true, "renderNavBar": false, "content": errHtml, diff --git a/src/pages/categories.go b/src/pages/categories.go index 8503f82..534c9af 100644 --- a/src/pages/categories.go +++ b/src/pages/categories.go @@ -45,7 +45,7 @@ func categoriesGlobalPage(c *gin.Context) { categories, ) - c.HTML(http.StatusOK, "terminal.gotempl", gin.H{ + c.HTML(http.StatusOK, "terminal.html", gin.H{ "page": "categories", "renderNavBar": true, "content": content, @@ -68,7 +68,7 @@ func categoryPage(c *gin.Context) { }, ) - c.HTML(http.StatusOK, "terminal.gotempl", gin.H{ + c.HTML(http.StatusOK, "terminal.html", gin.H{ "page": "category", "renderNavBar": false, "content": content, @@ -82,7 +82,7 @@ func newCategoryPage(c *gin.Context) { fp.Join(cfg.AssetsDir, "/htmx/categoryNew.html"), map[string]any{}, ) - c.HTML(http.StatusOK, "terminal.gotempl", gin.H{ + c.HTML(http.StatusOK, "terminal.html", gin.H{ "page": "expenseNew", "renderNavBar": false, "content": content, diff --git a/src/pages/expenses.go b/src/pages/expenses.go index a2a8352..147d224 100644 --- a/src/pages/expenses.go +++ b/src/pages/expenses.go @@ -2,14 +2,11 @@ package pages import ( "encoding/json" - "expenses/config" "expenses/expenses" - "expenses/templating" "fmt" "strconv" "net/http" - fp "path/filepath" "github.com/gin-gonic/gin" ) @@ -18,60 +15,34 @@ const ( ExpensesPath = "/expenses" ) -func expensesContent() (map[string]any, error) { - expenses, err := expenses.GetAllExpenses() - if err != nil { - return nil, err - } - - data := map[string]any{ - "size": len(expenses) - 1, - "expenses": expenses, - "resource": "expenses", - } - - return data, nil -} - func expensesPartial(c *gin.Context) { - cfg := config.GetInstance() - - expenses, err := expensesContent() + expenses, err := expenses.GetAllExpenses() if err != nil { c.Header("HX-Redirect", "/500") return } - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "htmx/expenses.html"), - expenses, - ) - c.String(http.StatusOK, string(content)) + c.HTML(http.StatusOK, "expenses", gin.H{ + "expenses": expenses, + }) } func ExpensesGlobalPage(c *gin.Context) { - cfg := config.GetInstance() - - expenses, err := expensesContent() + expenses, err := expenses.GetAllExpenses() if err != nil { c.Header("HX-Redirect", "/500") return } - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "htmx/expenses.html"), - expenses, - ) - c.HTML(http.StatusOK, "terminal.gotempl", gin.H{ + c.HTML(http.StatusOK, "terminal.html", gin.H{ "page": "expenses", "renderNavBar": true, - "content": content, + "content": "expenses", + "expenses": expenses, }) } func expensePage(c *gin.Context) { - cfg := config.GetInstance() - expenseID, err := strconv.Atoi(c.Param("id")) if err != nil { c.Redirect(404, "/404") @@ -120,34 +91,26 @@ func expensePage(c *gin.Context) { return } - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "/htmx/expense.html"), - map[string]any{ - "method": "put", - "expense": expense, - "categories": categories, - "stores": stores, - "types": types, - "summary": summary, - "users": users, - }, - ) - if expense.ExpID == -1 { NotFoundView(c, fmt.Sprintf("ID `%d` doesn't exist", expenseID)) return } - c.HTML(http.StatusOK, "terminal.gotempl", gin.H{ + c.HTML(http.StatusOK, "terminal.html", gin.H{ "page": "expense", "renderNavBar": false, - "content": content, + "content": "expense", + "method": "put", + "expense": expense, + "categories": categories, + "stores": stores, + "types": types, + "summary": summary, + "users": users, }) } func newExpensePage(c *gin.Context) { - cfg := config.GetInstance() - categories, err := expenses.GetAllCategories() if err != nil { ServerErrorView(c, "failed to fetch categories") @@ -172,21 +135,16 @@ func newExpensePage(c *gin.Context) { return } - content := templating.HtmlTemplate( - fp.Join(cfg.AssetsDir, "/htmx/expense.html"), map[string]any{ - "method": "post", - "expense": expenses.NewExpense(), - "categories": categories, - "stores": stores, - "types": types, - "users": users, - }, - ) - - c.HTML(http.StatusOK, "terminal.gotempl", gin.H{ + c.HTML(http.StatusOK, "terminal.html", gin.H{ "page": "expenseNew", "renderNavBar": false, - "content": content, + "content": "newExpense", + "method": "post", + "expense": expenses.NewExpense(), + "categories": categories, + "stores": stores, + "types": types, + "users": users, }) } @@ -242,7 +200,7 @@ func deleteExpense(c *gin.Context) { payment := expenses.Expense{ ExpID: expenseID, - } + } err = payment.Delete() if err == expenses.ErrNotFound { diff --git a/src/pages/overview.go b/src/pages/overview.go index 2ea828c..5a8f5af 100644 --- a/src/pages/overview.go +++ b/src/pages/overview.go @@ -43,7 +43,7 @@ func overviewPartialPage(c *gin.Context) { func overviewPage(c *gin.Context) { content := overviewContent() - c.HTML(http.StatusOK, "terminal.gotempl", gin.H{ + c.HTML(http.StatusOK, "terminal.html", gin.H{ "page": "overview", "renderNavBar": true, "content": content, diff --git a/src/pages/stores.go b/src/pages/stores.go index 8411b02..13f4244 100644 --- a/src/pages/stores.go +++ b/src/pages/stores.go @@ -45,7 +45,7 @@ func storesGlobalPage(c *gin.Context) { stores, ) - c.HTML(http.StatusOK, "terminal.gotempl", gin.H{ + c.HTML(http.StatusOK, "terminal.html", gin.H{ "page": "stores", "renderNavBar": true, "content": content, @@ -68,7 +68,7 @@ func storePage(c *gin.Context) { }, ) - c.HTML(http.StatusOK, "terminal.gotempl", gin.H{ + c.HTML(http.StatusOK, "terminal.html", gin.H{ "page": "store", "renderNavBar": false, "content": content, @@ -82,7 +82,7 @@ func newStorePage(c *gin.Context) { fp.Join(cfg.AssetsDir, "/htmx/storeNew.html"), map[string]any{}, ) - c.HTML(http.StatusOK, "terminal.gotempl", gin.H{ + c.HTML(http.StatusOK, "terminal.html", gin.H{ "page": "storeNew", "renderNavBar": false, "content": content, diff --git a/src/pages/types.go b/src/pages/types.go index 5a92aa8..8187220 100644 --- a/src/pages/types.go +++ b/src/pages/types.go @@ -45,7 +45,7 @@ func typesGlobalPage(c *gin.Context) { types, ) - c.HTML(http.StatusOK, "terminal.gotempl", gin.H{ + c.HTML(http.StatusOK, "terminal.html", gin.H{ "page": "types", "renderNavBar": true, "content": content, @@ -68,7 +68,7 @@ func typePage(c *gin.Context) { }, ) - c.HTML(http.StatusOK, "terminal.gotempl", gin.H{ + c.HTML(http.StatusOK, "terminal.html", gin.H{ "page": "type", "renderNavBar": false, "content": content, @@ -82,7 +82,7 @@ func newTypePage(c *gin.Context) { fp.Join(cfg.AssetsDir, "/htmx/typeNew.html"), map[string]any{}, ) - c.HTML(http.StatusOK, "terminal.gotempl", gin.H{ + c.HTML(http.StatusOK, "terminal.html", gin.H{ "page": "typeNew", "renderNavBar": false, "content": content, diff --git a/src/templating/templating.go b/src/templating/templating.go index f06805d..36fc1f9 100644 --- a/src/templating/templating.go +++ b/src/templating/templating.go @@ -2,6 +2,7 @@ package templating import ( "bytes" + "expenses/config" "fmt" "html/template" "log" @@ -9,8 +10,8 @@ import ( "time" ) -func createTemplateEngine(name string) *template.Template { - funcMap := template.FuncMap{ +var ( + TmplFuncMap = template.FuncMap{ "formatDate": func(ts int64) string { return time.Unix(ts, 0).Format("02-Jan-2006") }, @@ -18,8 +19,17 @@ func createTemplateEngine(name string) *template.Template { return fmt.Sprintf("%.2f", v) }, } +) - tmpl := template.New(name).Funcs(funcMap) +func createTemplateEngine(name string) *template.Template { + cfg := config.GetInstance() + + + tmpl := template.New(name).Funcs(TmplFuncMap) + tmpl, err := tmpl.ParseGlob(fp.Join(cfg.AssetsDir, "htmx/*.html")) + if err != nil { + panic(err) + } return tmpl }