Made all the templating work with gin instead
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
# TODO
|
||||
- [ ] On the categories page, add a column of how many times a given category is
|
||||
used
|
||||
- [ ] Make all the other items update/create page be like expenses
|
||||
@@ -30,7 +30,7 @@
|
||||
opacity: 1;
|
||||
}
|
||||
20% {
|
||||
opacity: 1.1;
|
||||
opacity: 1;
|
||||
}
|
||||
40% {
|
||||
opacity: 1;
|
||||
@@ -39,10 +39,10 @@
|
||||
opacity: 1;
|
||||
}
|
||||
80% {
|
||||
opacity: 1.1;
|
||||
opacity: 1;
|
||||
}
|
||||
100% {
|
||||
opacity: 0.9;
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
@keyframes beam {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
{{ define "categories" }}
|
||||
<div id="operation-state" class="warning"> </div><br>
|
||||
<table>
|
||||
<tr>
|
||||
@@ -50,3 +51,4 @@
|
||||
});
|
||||
})
|
||||
</script>
|
||||
{{ end }}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
{{ define "category" }}
|
||||
<table>
|
||||
<tr>
|
||||
<td>ID:</td>
|
||||
@@ -8,3 +9,4 @@
|
||||
<td>{{ .category.CategoryName }}</td>
|
||||
</tr>
|
||||
</table>
|
||||
{{ end }}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
{{ define "categoryNew" }}
|
||||
<div>
|
||||
<form id="category-form" action="submit">
|
||||
<fieldset id="form-box-boundaries">
|
||||
@@ -19,3 +20,4 @@
|
||||
</fieldset>
|
||||
</form>
|
||||
</div>
|
||||
{{ end }}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
{{ define "overview" }}
|
||||
<form>
|
||||
<p>
|
||||
<label for="range-start">Start date:</label>
|
||||
@@ -24,3 +25,4 @@
|
||||
|
||||
<div id="query-results">
|
||||
</div>
|
||||
{{ end }}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
{{ define "overviewResults" }}
|
||||
{{ range .expenses }}
|
||||
<table>
|
||||
<tr>
|
||||
@@ -42,3 +43,4 @@
|
||||
</tr>
|
||||
{{ end }}
|
||||
</table>
|
||||
{{ end }}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
{{ define "panic" }}
|
||||
<code class="warning">
|
||||
{{ .msg }}
|
||||
<br>
|
||||
@@ -31,3 +32,4 @@
|
||||
/usr/lib/go/src/runtime/asm_amd64.s:1695 (0x472d80)
|
||||
goexit: BYTE $0x90 // NOP
|
||||
</code>
|
||||
{{ end }}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
{{ define "store" }}
|
||||
<table>
|
||||
<tr>
|
||||
<td>ID:</td>
|
||||
<td>{{ .store.StoreID }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Name:</td>
|
||||
<td>{{ .store.StoreName }}</td>
|
||||
</tr>
|
||||
</table>
|
||||
{{ end }}
|
||||
@@ -1,7 +1,8 @@
|
||||
{{ define "storeNew" }}
|
||||
<div>
|
||||
<form id="store-form" action="submit">
|
||||
<fieldset id="form-box-boundaries">
|
||||
<legend>CATEGORY FORM</legend>
|
||||
<legend>STORE FORM</legend>
|
||||
<div class="form-container form-left">
|
||||
<div id="form-state" class="warning"></div><br>
|
||||
|
||||
@@ -24,3 +25,4 @@
|
||||
</fieldset>
|
||||
</form>
|
||||
</div>
|
||||
{{ end }}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
{{ define "stores" }}
|
||||
<div id="operation-state" class="warning"> </div><br>
|
||||
<table>
|
||||
<tr>
|
||||
@@ -50,3 +51,4 @@
|
||||
});
|
||||
})
|
||||
</script>
|
||||
{{ end }}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
{{ define "terminal" }}
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
@@ -53,6 +54,26 @@
|
||||
{{ template "expenses" . }}
|
||||
{{ else if eq .content "newExpense" }}
|
||||
{{ template "expense" . }}
|
||||
{{ else if eq .content "panic" }}
|
||||
{{ template "panic" . }}
|
||||
{{ else if eq .content "categories" }}
|
||||
{{ template "categories" . }}
|
||||
{{ else if eq .content "category" }}
|
||||
{{ template "category" . }}
|
||||
{{ else if eq .content "categoryNew" }}
|
||||
{{ template "categoryNew" . }}
|
||||
{{ else if eq .content "overview" }}
|
||||
{{ template "overview" . }}
|
||||
{{ else if eq .content "stores" }}
|
||||
{{ template "stores" . }}
|
||||
{{ else if eq .content "store" }}
|
||||
{{ template "store" . }}
|
||||
{{ else if eq .content "storeNew" }}
|
||||
{{ template "storeNew" . }}
|
||||
{{ else if eq .content "types" }}
|
||||
{{ template "types" . }}
|
||||
{{ else if eq .content "typeNew" }}
|
||||
{{ template "typeNew" . }}
|
||||
{{ else }}
|
||||
<div>Unknown page</div>
|
||||
{{ end }}
|
||||
@@ -65,3 +86,4 @@
|
||||
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
{{ define "type" }}
|
||||
<div>
|
||||
<form id="type-form" action="submit">
|
||||
<fieldset id="form-box-boundaries">
|
||||
@@ -33,3 +34,4 @@
|
||||
</fieldset>
|
||||
</form>
|
||||
</div>
|
||||
{{ end }}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
{{ define "typeNew" }}
|
||||
<div>
|
||||
<form id="type-form" action="submit">
|
||||
<fieldset id="form-box-boundaries">
|
||||
@@ -19,3 +20,4 @@
|
||||
</fieldset>
|
||||
</form>
|
||||
</div>
|
||||
{{ end }}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
{{ define "types" }}
|
||||
<div id="operation-state" class="warning"> </div><br>
|
||||
<table>
|
||||
<tr>
|
||||
@@ -50,3 +51,4 @@
|
||||
});
|
||||
})
|
||||
</script>
|
||||
{{ end }}
|
||||
|
||||
+13
-3
@@ -3,12 +3,13 @@ package main
|
||||
import (
|
||||
"expenses/config"
|
||||
"expenses/pages"
|
||||
"expenses/templating"
|
||||
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
fp "path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -16,6 +17,15 @@ import (
|
||||
// This can be set in the compilation command - in the Makefile
|
||||
var ginMode = "debug"
|
||||
|
||||
var tmplFuncMap = template.FuncMap{
|
||||
"formatDate": func(ts int64) string {
|
||||
return time.Unix(ts, 0).Format("02-Jan-2006")
|
||||
},
|
||||
"formatPrice": func(v float32) string {
|
||||
return fmt.Sprintf("%.2f", v)
|
||||
},
|
||||
}
|
||||
|
||||
func main() {
|
||||
config.SetConfig("./config.yaml")
|
||||
cfg := config.GetInstance()
|
||||
@@ -23,8 +33,8 @@ func main() {
|
||||
gin.SetMode(ginMode)
|
||||
templates := template.Must(
|
||||
template.New("").
|
||||
Funcs(templating.TmplFuncMap).
|
||||
ParseGlob(fp.Join(cfg.AssetsDir, "htmx/*.html")),
|
||||
Funcs(tmplFuncMap).
|
||||
ParseGlob(fp.Join(cfg.AssetsDir, "htmx/*.html")),
|
||||
)
|
||||
|
||||
router := gin.Default()
|
||||
|
||||
+3
-13
@@ -1,27 +1,17 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"expenses/config"
|
||||
"expenses/templating"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func NotFoundView(c *gin.Context, msg string) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
errHtml := templating.HtmlTemplate(
|
||||
cfg.AssetsDir+"htmx/panic.html", map[string]any{
|
||||
"msg": msg,
|
||||
},
|
||||
)
|
||||
|
||||
c.HTML(404, "terminal.html", gin.H{
|
||||
c.HTML(404, "terminal", gin.H{
|
||||
"warning": true,
|
||||
"renderNavBar": false,
|
||||
"content": errHtml,
|
||||
"content": "panic",
|
||||
"msg": msg,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+3
-13
@@ -1,25 +1,15 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"expenses/config"
|
||||
"expenses/templating"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ServerErrorView(c *gin.Context, msg string) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
errHtml := templating.HtmlTemplate(
|
||||
cfg.AssetsDir+"htmx/panic.html", map[string]any{
|
||||
"msg": msg,
|
||||
},
|
||||
)
|
||||
|
||||
c.HTML(500, "terminal.html", gin.H{
|
||||
c.HTML(500, "terminal", gin.H{
|
||||
"warning": true,
|
||||
"renderNavBar": false,
|
||||
"content": errHtml,
|
||||
"content": "panic",
|
||||
"msg": msg,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+18
-48
@@ -1,13 +1,10 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"expenses/config"
|
||||
"expenses/expenses"
|
||||
"expenses/templating"
|
||||
|
||||
"fmt"
|
||||
"net/http"
|
||||
fp "path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -17,75 +14,47 @@ const (
|
||||
CategoriesPath = "/categories"
|
||||
)
|
||||
|
||||
func categoriesContent() (map[string]any, error) {
|
||||
func categoriesGlobalPage(c *gin.Context) {
|
||||
categories, err := expenses.GetAllCategories()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data := map[string]any{
|
||||
"size": len(categories) - 1,
|
||||
"categories": categories,
|
||||
"resource": "categories",
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func categoriesGlobalPage(c *gin.Context) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
categories, err := categoriesContent()
|
||||
if err != nil {
|
||||
c.Header("HX-Redirect", "/500")
|
||||
ServerErrorView(c, "The server too makes mistakes")
|
||||
return
|
||||
}
|
||||
content := templating.HtmlTemplate(
|
||||
fp.Join(cfg.AssetsDir, "htmx/categories.html"),
|
||||
categories,
|
||||
)
|
||||
|
||||
c.HTML(http.StatusOK, "terminal.html", gin.H{
|
||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||
"page": "categories",
|
||||
"renderNavBar": true,
|
||||
"content": content,
|
||||
"content": "categories",
|
||||
"categories": categories,
|
||||
})
|
||||
}
|
||||
|
||||
func categoryPage(c *gin.Context) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
categoryID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.Redirect(404, "/404")
|
||||
NotFoundView(c, "No such category")
|
||||
return
|
||||
}
|
||||
|
||||
category, err := expenses.GetCategory(categoryID)
|
||||
content := templating.HtmlTemplate(
|
||||
fp.Join(cfg.AssetsDir, "/htmx/category.html"),
|
||||
map[string]any{
|
||||
"category": category,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "The server too makes mistakes")
|
||||
return
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "terminal.html", gin.H{
|
||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||
"page": "category",
|
||||
"renderNavBar": false,
|
||||
"content": content,
|
||||
"content": "category",
|
||||
"category": category,
|
||||
})
|
||||
}
|
||||
|
||||
func newCategoryPage(c *gin.Context) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
content := templating.HtmlTemplate(
|
||||
fp.Join(cfg.AssetsDir, "/htmx/categoryNew.html"), map[string]any{},
|
||||
)
|
||||
|
||||
c.HTML(http.StatusOK, "terminal.html", gin.H{
|
||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||
"page": "expenseNew",
|
||||
"renderNavBar": false,
|
||||
"content": content,
|
||||
"content": "categoryNew",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -107,7 +76,8 @@ func createCategory(c *gin.Context) {
|
||||
func deleteCategory(c *gin.Context) {
|
||||
categoryID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.Redirect(404, "/404")
|
||||
NotFoundView(c, "No such category")
|
||||
return
|
||||
}
|
||||
|
||||
category := expenses.Category{
|
||||
|
||||
@@ -34,7 +34,7 @@ func ExpensesGlobalPage(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "terminal.html", gin.H{
|
||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||
"page": "expenses",
|
||||
"renderNavBar": true,
|
||||
"content": "expenses",
|
||||
@@ -96,7 +96,7 @@ func expensePage(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "terminal.html", gin.H{
|
||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||
"page": "expense",
|
||||
"renderNavBar": false,
|
||||
"content": "expense",
|
||||
@@ -135,7 +135,7 @@ func newExpensePage(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "terminal.html", gin.H{
|
||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||
"page": "expenseNew",
|
||||
"renderNavBar": false,
|
||||
"content": "newExpense",
|
||||
|
||||
+7
-32
@@ -1,15 +1,11 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"expenses/config"
|
||||
"expenses/expenses"
|
||||
"expenses/templating"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"html/template"
|
||||
"net/http"
|
||||
fp "path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -24,35 +20,19 @@ type UserDebtSummary struct {
|
||||
Total float32
|
||||
}
|
||||
|
||||
func overviewContent() template.HTML {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
content := templating.HtmlTemplate(
|
||||
fp.Join(cfg.AssetsDir, "htmx/overview.html"),
|
||||
map[string]any{},
|
||||
)
|
||||
|
||||
return content
|
||||
}
|
||||
|
||||
func overviewPartialPage(c *gin.Context) {
|
||||
content := overviewContent()
|
||||
c.String(http.StatusOK, string(content))
|
||||
c.HTML(http.StatusOK, "overview", gin.H{})
|
||||
}
|
||||
|
||||
func overviewPage(c *gin.Context) {
|
||||
content := overviewContent()
|
||||
|
||||
c.HTML(http.StatusOK, "terminal.html", gin.H{
|
||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||
"page": "overview",
|
||||
"renderNavBar": true,
|
||||
"content": content,
|
||||
"content": "overview",
|
||||
})
|
||||
}
|
||||
|
||||
func getResults(c *gin.Context) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
startDateStr := c.PostForm("range-start") + " 00:00:00"
|
||||
endDateStr := c.PostForm("range-end") + " 23:59:59"
|
||||
|
||||
@@ -115,15 +95,10 @@ func getResults(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
content := templating.HtmlTemplate(
|
||||
fp.Join(cfg.AssetsDir, "htmx/overviewResults.html"),
|
||||
map[string]any{
|
||||
"expenses": expensesWithDebts,
|
||||
"summary": userDebtSummary,
|
||||
},
|
||||
)
|
||||
|
||||
c.String(http.StatusOK, string(content))
|
||||
c.HTML(http.StatusOK, "overviewResults", gin.H{
|
||||
"expenses": expensesWithDebts,
|
||||
"summary": userDebtSummary,
|
||||
})
|
||||
}
|
||||
|
||||
func RouteOverview(router *gin.Engine) {
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
func deletePayment(c *gin.Context) {
|
||||
paymentID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.Header("HX-Redirect", "/404")
|
||||
NotFoundView(c, "failed to fetch payment")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+13
-48
@@ -1,13 +1,10 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"expenses/config"
|
||||
"expenses/expenses"
|
||||
"expenses/templating"
|
||||
"fmt"
|
||||
|
||||
"net/http"
|
||||
fp "path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -17,75 +14,43 @@ const (
|
||||
StoresPath = "/stores"
|
||||
)
|
||||
|
||||
func storesContent() (map[string]any, error) {
|
||||
func storesGlobalPage(c *gin.Context) {
|
||||
stores, err := expenses.GetAllStores()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data := map[string]any{
|
||||
"size": len(stores) - 1,
|
||||
"stores": stores,
|
||||
"resource": "stores",
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func storesGlobalPage(c *gin.Context) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
stores, err := storesContent()
|
||||
if err != nil {
|
||||
c.Header("HX-Redirect", "/500")
|
||||
ServerErrorView(c, "Failed to fetch stores content")
|
||||
return
|
||||
}
|
||||
content := templating.HtmlTemplate(
|
||||
fp.Join(cfg.AssetsDir, "htmx/stores.html"),
|
||||
stores,
|
||||
)
|
||||
|
||||
c.HTML(http.StatusOK, "terminal.html", gin.H{
|
||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||
"page": "stores",
|
||||
"renderNavBar": true,
|
||||
"content": content,
|
||||
"content": "stores",
|
||||
"stores": stores,
|
||||
})
|
||||
}
|
||||
|
||||
func storePage(c *gin.Context) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
storeID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.Redirect(404, "/404")
|
||||
NotFoundView(c, "didn't find request store")
|
||||
return
|
||||
}
|
||||
|
||||
store, err := expenses.GetCategory(storeID)
|
||||
content := templating.HtmlTemplate(
|
||||
fp.Join(cfg.AssetsDir, "/htmx/category.html"),
|
||||
map[string]any{
|
||||
"store": store,
|
||||
},
|
||||
)
|
||||
store, err := expenses.GetStore(storeID)
|
||||
|
||||
c.HTML(http.StatusOK, "terminal.html", gin.H{
|
||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||
"page": "store",
|
||||
"renderNavBar": false,
|
||||
"content": content,
|
||||
"content": "store",
|
||||
"store": store,
|
||||
})
|
||||
}
|
||||
|
||||
func newStorePage(c *gin.Context) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
content := templating.HtmlTemplate(
|
||||
fp.Join(cfg.AssetsDir, "/htmx/storeNew.html"), map[string]any{},
|
||||
)
|
||||
|
||||
c.HTML(http.StatusOK, "terminal.html", gin.H{
|
||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||
"page": "storeNew",
|
||||
"renderNavBar": false,
|
||||
"content": content,
|
||||
"content": "storeNew",
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+15
-47
@@ -1,13 +1,10 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"expenses/config"
|
||||
"expenses/expenses"
|
||||
"expenses/templating"
|
||||
|
||||
"fmt"
|
||||
"net/http"
|
||||
fp "path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -17,75 +14,46 @@ const (
|
||||
TypesPath = "/types"
|
||||
)
|
||||
|
||||
func typesContent() (map[string]any, error) {
|
||||
func typesGlobalPage(c *gin.Context) {
|
||||
types, err := expenses.GetAllTypes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data := map[string]any{
|
||||
"size": len(types) - 1,
|
||||
"types": types,
|
||||
"resource": "types",
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func typesGlobalPage(c *gin.Context) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
types, err := typesContent()
|
||||
if err != nil {
|
||||
c.Header("HX-Redirect", "/500")
|
||||
ServerErrorView(c, "the server failed to get the types")
|
||||
return
|
||||
}
|
||||
content := templating.HtmlTemplate(
|
||||
fp.Join(cfg.AssetsDir, "htmx/types.html"),
|
||||
types,
|
||||
)
|
||||
|
||||
c.HTML(http.StatusOK, "terminal.html", gin.H{
|
||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||
"page": "types",
|
||||
"renderNavBar": true,
|
||||
"content": content,
|
||||
"content": "types",
|
||||
"types": types,
|
||||
})
|
||||
}
|
||||
|
||||
func typePage(c *gin.Context) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
typeID, err := strconv.Atoi(c.Param("id"))
|
||||
if err != nil {
|
||||
c.Redirect(404, "/404")
|
||||
NotFoundView(c, "requested type doesn't exist")
|
||||
return
|
||||
}
|
||||
|
||||
typ, err := expenses.GetType(typeID)
|
||||
content := templating.HtmlTemplate(
|
||||
fp.Join(cfg.AssetsDir, "/htmx/type.html"),
|
||||
map[string]any{
|
||||
"type": typ,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
ServerErrorView(c, "failed to retrieve requested type")
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "terminal.html", gin.H{
|
||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||
"page": "type",
|
||||
"renderNavBar": false,
|
||||
"content": content,
|
||||
"content": "type",
|
||||
"type": typ,
|
||||
})
|
||||
}
|
||||
|
||||
func newTypePage(c *gin.Context) {
|
||||
cfg := config.GetInstance()
|
||||
|
||||
content := templating.HtmlTemplate(
|
||||
fp.Join(cfg.AssetsDir, "/htmx/typeNew.html"), map[string]any{},
|
||||
)
|
||||
|
||||
c.HTML(http.StatusOK, "terminal.html", gin.H{
|
||||
c.HTML(http.StatusOK, "terminal", gin.H{
|
||||
"page": "typeNew",
|
||||
"renderNavBar": false,
|
||||
"content": content,
|
||||
"content": "typeNew",
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
package templating
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"expenses/config"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
fp "path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
TmplFuncMap = template.FuncMap{
|
||||
"formatDate": func(ts int64) string {
|
||||
return time.Unix(ts, 0).Format("02-Jan-2006")
|
||||
},
|
||||
"formatPrice": func(v float32) string {
|
||||
return fmt.Sprintf("%.2f", v)
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func HtmlTemplate(file string, data map[string]any) template.HTML {
|
||||
tmplEngName := fp.Base(file)
|
||||
tmplEngine := createTemplateEngine(tmplEngName)
|
||||
|
||||
tmpl, err := tmplEngine.ParseFiles(file)
|
||||
if err != nil {
|
||||
log.Println("error in templating")
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Use a bytes.Buffer to store the rendered output
|
||||
var renderedOutput bytes.Buffer
|
||||
|
||||
// Render the template
|
||||
err = tmpl.ExecuteTemplate(&renderedOutput, tmplEngName, data)
|
||||
if err != nil {
|
||||
log.Fatalf("Template execution: %v", err)
|
||||
}
|
||||
|
||||
// Now renderedOutput holds the rendered template
|
||||
return template.HTML(renderedOutput.String())
|
||||
}
|
||||
Reference in New Issue
Block a user