From 526a1c4faadd5cbf7d9cccda44891968995173fc Mon Sep 17 00:00:00 2001 From: Wancat Date: Thu, 20 Oct 2022 11:20:24 +0800 Subject: [PATCH] Migrate to layout --- main.go | 16 +++++-------- template.go | 27 +++++++++++++++++++++ templates/index.html | 47 ++++++++++++++++--------------------- templates/layouts/base.html | 12 ++++++++++ templates/new.html | 22 +++++++---------- templates/success.html | 19 +++++---------- 6 files changed, 79 insertions(+), 64 deletions(-) create mode 100644 template.go create mode 100644 templates/layouts/base.html diff --git a/main.go b/main.go index b338ee4..9d91215 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,6 @@ type TxData struct { func init() { ledgerTpl = template.Must(template.ParseGlob("tx/*")) - htmlTpl = template.Must(template.ParseGlob("templates/*.html")) flag.StringVar(&LEDGER_FILE, "f", "example.txt", "ledger journal file to write") flag.StringVar(&LEDGER_INIT, "i", "", "ledger initiation file") flag.StringVar(&WORKING_DIR, "w", "", "ledger working directory") @@ -43,8 +42,9 @@ func init() { func main() { r := gin.Default() + r.HTMLRender = loadTemplates("templates") r.GET("/", func(c *gin.Context) { - htmlTpl.ExecuteTemplate(c.Writer, "index.html", struct { + c.HTML(200, "index.html", struct { Templates []*template.Template Scripts map[string][]string }{ @@ -65,11 +65,9 @@ func main() { log.Println(err, c.Request.Form) return } - if err := htmlTpl.ExecuteTemplate(c.Writer, "new.html", struct { + c.HTML(200, "new.html", struct { Tx string - }{tx}); err != nil { - c.AbortWithError(500, err) - } + }{tx}) }) r.POST("/submit", func(c *gin.Context) { @@ -79,11 +77,9 @@ func main() { return } - if err := htmlTpl.ExecuteTemplate(c.Writer, "success.html", struct { + c.HTML(200, "success.html", struct { Tx string - }{tx}); err != nil { - c.AbortWithError(500, err) - } + }{tx}) }) r.GET("/exec", func(c *gin.Context) { diff --git a/template.go b/template.go new file mode 100644 index 0000000..f01e8ba --- /dev/null +++ b/template.go @@ -0,0 +1,27 @@ +package main + +import ( + "path" + "path/filepath" + + "github.com/gin-contrib/multitemplate" +) + +func loadTemplates(templatesDir string) multitemplate.Renderer { + r := multitemplate.NewRenderer() + + layouts, err := filepath.Glob(path.Join(templatesDir, "layouts", "*.html")) + if err != nil { + panic(err) + } + + includes, err := filepath.Glob(path.Join(templatesDir, "*.html")) + if err != nil { + panic(err) + } + + for _, include := range includes { + r.AddFromFiles(filepath.Base(include), append(layouts, include)...) + } + return r +} diff --git a/templates/index.html b/templates/index.html index f8d13f8..7db6bf7 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,27 +1,20 @@ - - - - Ledger Quick Note - - - -

Ledger Quick Note

-
-
-
-
-
- -
-

Scripts

- - - +{{ define "main" }} +

Ledger Quick Note

+
+
+
+
+
+ +
+

Scripts

+ +{{ end }} diff --git a/templates/layouts/base.html b/templates/layouts/base.html new file mode 100644 index 0000000..a9f12de --- /dev/null +++ b/templates/layouts/base.html @@ -0,0 +1,12 @@ + + + + {{ block "title" . }}Ledger Quick Note{{ end }} + + + + {{ block "main" . }} + + {{ end }} + + diff --git a/templates/new.html b/templates/new.html index 2b1288e..1f9a0cb 100644 --- a/templates/new.html +++ b/templates/new.html @@ -1,14 +1,8 @@ - - - - Ledger Quick Note - - - -

Confirm new Tx

-
-
- -
- - +{{ define "title" }}Confirm{{ end }} +{{ define "main" }} +

Confirm new Tx

+
+
+ +
+{{ end }} diff --git a/templates/success.html b/templates/success.html index 02d3460..e50e5b6 100644 --- a/templates/success.html +++ b/templates/success.html @@ -1,13 +1,6 @@ - - - - Ledger Quick Note - - - -

Ledger Quick Note

-

Success

-
{{ .Tx }}
-

Back to home

- - +{{ define "title" }}Success{{ end }} +{{ define "main" }} +

Success

+
{{ .Tx }}
+

Back to home

+{{ end }}