Getting rid of the templating subpackage, complete unecessary

This commit is contained in:
2025-07-28 03:34:26 +01:00
parent af9265575b
commit 0d373d1990
15 changed files with 219 additions and 231 deletions
+4 -139
View File
@@ -1,3 +1,4 @@
{{ define "expense" }}
<div>
<form id="expense-form" action="submit">
<div id="form-state" class="warning"></div>
@@ -58,144 +59,12 @@
<fieldset id="shares-table-fields">
<legend>SHARES</legend>
<button
type="button"
onclick="addShareRow()"
>+ Share</button>
<table id="shares-table">
<tr>
<th>ID</th>
<th>USER</th>
<th>SHARE</th>
</tr>
<!-- Data row to be copied -->
<tr id="template-share-row" style="display: none;">
<td>
<input name="shares-ids[]" disabled readonly
value="" class="tui-dropdown">
</td>
<td>
<select name="shares-user-ids[]" disabled class="tui-dropdown">
{{ range .users }}
<option
{{ if eq .UserID $.expense.OwnerUser.UserID }}selected{{ end }}
value="{{ .UserID }}">{{ .UserName }}</option>
{{ end }}
</select>
</td>
<td>
<input type="number" name="shares-percent[]"
value="1" disabled class="tui-dropdown">
</td>
<td class="row-delete-button">
<button type="button" onclick="deleteShareRow(this)">X</button>
</td>
</tr>
{{ range .expense.Shares }}
{{ $share := . }}
<tr>
<td>
<input
name="shares-ids[]" readonly
value="{{ $share.ExpShareID }}" class="tui-dropdown">
</td>
<td>
<select name="shares-user-ids[]" class="tui-dropdown">
{{ range $.users }}
<option
value="{{ .UserID }}"
{{ if eq .UserID $share.User.UserID }}selected{{ end }}>
{{ .UserName }}
</option>
{{ end }}
</select>
</td>
<td>
<input type="number" name="shares-percent[]"
value="{{ $share.Share }}" class="tui-dropdown">
</td>
<td class="row-delete-button">
<button type="button" onclick="deleteShareRow(this)">X</button>
</td>
</tr>
{{ end }}
</table>
{{ template "expenseShares" . }}
</fieldset>
<fieldset>
<legend>PAYMENTS</legend>
<button
type="button"
onclick="addPaymentRow()"
>+ Payment</button>
<div id="operation-state" class="warning">&nbsp;</div><br>
<table id="payments-table">
<tr>
<th>ID</th>
<th>USER</th>
<th>PAYED</th>
</tr>
<!-- Data row to be copied -->
<tr id="template-payment-row" style="display: none;">
<td >
<input name="payments-ids[]" disabled readonly
value="" class="tui-dropdown">
</td>
<td>
<select name="payments-user-ids[]" disabled class="tui-dropdown">
{{ range .users }}
<option
{{ if eq .UserID $.expense.OwnerUser.UserID }}selected{{ end }}
value="{{ .UserID }}">{{ .UserName }}</option>
{{ end }}
</select>
</td>
<td>
<input type="number" name="payments-payment[]" value="0" disabled class="tui-dropdown">
</td>
<td class="row-delete-button">
<button type="button" onclick="deleteShareRow(this)">X</button>
</td>
</tr>
{{ range .expense.Payments }}
{{ $payment := . }}
<tr data-id="{{ $payment.ExpPaymID }}">
<td>
<input name="payments-ids[]" readonly
value="{{ $payment.ExpPaymID }}" class="tui-dropdown">
</td>
<td>
<select name="payments-user-ids[]" class="tui-dropdown">
{{ range $.users }}
<option
value="{{ .UserID }}"
{{ if eq .UserID $payment.User.UserID }}selected{{ end }}>
{{ .UserName }}
</option>
{{ end }}
</select>
</td>
<td>
<input type="number" name="payments-payment[]"
value="{{ $payment.PayedAmount }}" class="tui-dropdown">
</td>
<td class="row-delete-button">
<!-- <button type="button" onclick="deleteShareRow(this)">X</button> -->
<button
type="button"
class="delete-button"
hx-delete="/payments/{{ $payment.ExpPaymID }}"
hx-target="closest tr">X</button>
</td>
</tr>
{{ end }}
</table>
{{ template "expensePayments" . }}
</fieldset>
<br>
@@ -213,11 +82,6 @@
{{- if eq .method "put" }}UPDATE{{- end }}
{{- if eq .method "post" }}CREATE{{- end }}</button>
{{ if eq .method "put" }}
<!-- <button -->
<!-- hx-delete="/expenses/{{ .expense.ExpID }}" -->
<!-- hx-swap="none" -->
<!-- type="button" -->
<!-- id="expense-delete-button">DELETE</button> -->
<button
onclick="confirmDelete(this)"
type="button"
@@ -276,3 +140,4 @@
deleteTarget = null;
});
</script>
{{ end }}
+72
View File
@@ -0,0 +1,72 @@
{{ define "expensePayments" }}
<button
type="button"
onclick="addPaymentRow()"
>+ Payment</button>
<div id="operation-state" class="warning">&nbsp;</div><br>
<table id="payments-table">
<tr>
<th>ID</th>
<th>USER</th>
<th>PAYED</th>
</tr>
<!-- Data row to be copied -->
<tr id="template-payment-row" style="display: none;">
<td >
<input name="payments-ids[]" disabled readonly
value="" class="tui-dropdown">
</td>
<td>
<select name="payments-user-ids[]" disabled class="tui-dropdown">
{{ range .users }}
<option
{{ if eq .UserID $.expense.OwnerUser.UserID }}selected{{ end }}
value="{{ .UserID }}">{{ .UserName }}</option>
{{ end }}
</select>
</td>
<td>
<input type="number" name="payments-payment[]" value="0" disabled class="tui-dropdown">
</td>
<td class="row-delete-button">
<button type="button" onclick="deleteShareRow(this)">X</button>
</td>
</tr>
{{ range .expense.Payments }}
{{ $payment := . }}
<tr data-id="{{ $payment.ExpPaymID }}">
<td>
<input name="payments-ids[]" readonly
value="{{ $payment.ExpPaymID }}" class="tui-dropdown">
</td>
<td>
<select name="payments-user-ids[]" class="tui-dropdown">
{{ range $.users }}
<option
value="{{ .UserID }}"
{{ if eq .UserID $payment.User.UserID }}selected{{ end }}>
{{ .UserName }}
</option>
{{ end }}
</select>
</td>
<td>
<input type="number" name="payments-payment[]"
value="{{ $payment.PayedAmount }}" class="tui-dropdown">
</td>
<td class="row-delete-button">
<!-- <button type="button" onclick="deleteShareRow(this)">X</button> -->
<button
type="button"
class="delete-button"
hx-delete="/payments/{{ $payment.ExpPaymID }}"
hx-target="closest tr">X</button>
</td>
</tr>
{{ end }}
</table>
{{ end }}
+66
View File
@@ -0,0 +1,66 @@
{{ define "expenseShares" }}
<button
type="button"
onclick="addShareRow()"
>+ Share</button>
<table id="shares-table">
<tr>
<th>ID</th>
<th>USER</th>
<th>SHARE</th>
</tr>
<!-- Data row to be copied -->
<tr id="template-share-row" style="display: none;">
<td>
<input name="shares-ids[]" disabled readonly
value="" class="tui-dropdown">
</td>
<td>
<select name="shares-user-ids[]" disabled class="tui-dropdown">
{{ range .users }}
<option
{{ if eq .UserID $.expense.OwnerUser.UserID }}selected{{ end }}
value="{{ .UserID }}">{{ .UserName }}</option>
{{ end }}
</select>
</td>
<td>
<input type="number" name="shares-percent[]"
value="1" disabled class="tui-dropdown">
</td>
<td class="row-delete-button">
<button type="button" onclick="deleteShareRow(this)">X</button>
</td>
</tr>
{{ range .expense.Shares }}
{{ $share := . }}
<tr>
<td>
<input
name="shares-ids[]" readonly
value="{{ $share.ExpShareID }}" class="tui-dropdown">
</td>
<td>
<select name="shares-user-ids[]" class="tui-dropdown">
{{ range $.users }}
<option
value="{{ .UserID }}"
{{ if eq .UserID $share.User.UserID }}selected{{ end }}>
{{ .UserName }}
</option>
{{ end }}
</select>
</td>
<td>
<input type="number" name="shares-percent[]"
value="{{ $share.Share }}" class="tui-dropdown">
</td>
<td class="row-delete-button">
<button type="button" onclick="deleteShareRow(this)">X</button>
</td>
</tr>
{{ end }}
</table>
{{ end }}
+2
View File
@@ -1,3 +1,4 @@
{{ define "expenses" }}
<table>
<tr>
<th>ID</th>
@@ -20,3 +21,4 @@
</tr>
{{ end }}
</table>
{{ end }}
@@ -47,7 +47,15 @@
{{ end }}
<main id="content">
{{ .content }}
{{ if eq .content "expense" }}
{{ template "expense" . }}
{{ else if eq .content "expenses" }}
{{ template "expenses" . }}
{{ else if eq .content "newExpense" }}
{{ template "expense" . }}
{{ else }}
<div>Unknown page</div>
{{ end }}
</main>
</div>
+13 -6
View File
@@ -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)
+2 -2
View File
@@ -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,
+2 -2
View File
@@ -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,
+3 -3
View File
@@ -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,
+26 -68
View File
@@ -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 {
+1 -1
View File
@@ -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,
+3 -3
View File
@@ -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,
+3 -3
View File
@@ -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,
+13 -3
View File
@@ -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
}