Migrate to layout
This commit is contained in:
16
main.go
16
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) {
|
||||
|
||||
27
template.go
Normal file
27
template.go
Normal file
@@ -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
|
||||
}
|
||||
@@ -1,27 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Ledger Quick Note</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Ledger Quick Note</h1>
|
||||
<form action="/new" method="POST">
|
||||
<label>Action:
|
||||
{{ range .Templates }}
|
||||
<input type="radio" name="action" value="{{ .Name }}">{{ .Name }}</option>
|
||||
{{ end }}
|
||||
</label><br>
|
||||
<label>Amount: <input name="amount" type="number"></label><br>
|
||||
<label>Account: <input name="account" type="text"></label></br>
|
||||
<label>Tx Name: <input name="name" type="text"></label></br>
|
||||
<input type="submit">
|
||||
</form>
|
||||
<h2>Scripts</h2>
|
||||
<ul>
|
||||
{{ range $k, $v := .Scripts }}
|
||||
<li><a href="/exec?name={{ $k }}">{{ $k }}</a></li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
{{ define "main" }}
|
||||
<h1>Ledger Quick Note</h1>
|
||||
<form action="/new" method="POST">
|
||||
<label>Action:
|
||||
{{ range .Templates }}
|
||||
<input type="radio" name="action" value="{{ .Name }}">{{ .Name }}</option>
|
||||
{{ end }}
|
||||
</label><br>
|
||||
<label>Amount: <input name="amount" type="number"></label><br>
|
||||
<label>Account: <input name="account" type="text"></label></br>
|
||||
<label>Tx Name: <input name="name" type="text"></label></br>
|
||||
<input type="submit">
|
||||
</form>
|
||||
<h2>Scripts</h2>
|
||||
<ul>
|
||||
{{ range $k, $v := .Scripts }}
|
||||
<li><a href="/exec?name={{ $k }}">{{ $k }}</a></li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
{{ end }}
|
||||
|
||||
12
templates/layouts/base.html
Normal file
12
templates/layouts/base.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>{{ block "title" . }}Ledger Quick Note{{ end }}</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
</head>
|
||||
<body>
|
||||
{{ block "main" . }}
|
||||
|
||||
{{ end }}
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,14 +1,8 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Ledger Quick Note</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Confirm new Tx</h1>
|
||||
<form action="/submit" method="POST">
|
||||
<textarea name="tx" rows="15" cols="40">{{ .Tx }}</textarea><br>
|
||||
<input type="submit">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
{{ define "title" }}Confirm{{ end }}
|
||||
{{ define "main" }}
|
||||
<h1>Confirm new Tx</h1>
|
||||
<form action="/submit" method="POST">
|
||||
<textarea name="tx" rows="15" cols="40">{{ .Tx }}</textarea><br>
|
||||
<input type="submit">
|
||||
</form>
|
||||
{{ end }}
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Ledger Quick Note</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Ledger Quick Note</h1>
|
||||
<p><strong>Success</strong></p>
|
||||
<pre><code>{{ .Tx }}</code></pre>
|
||||
<p><a href="/">Back to home</a></p>
|
||||
</body>
|
||||
</html>
|
||||
{{ define "title" }}Success{{ end }}
|
||||
{{ define "main" }}
|
||||
<p><strong>Success</strong></p>
|
||||
<pre><code>{{ .Tx }}</code></pre>
|
||||
<p><a href="/">Back to home</a></p>
|
||||
{{ end }}
|
||||
|
||||
Reference in New Issue
Block a user