Define tx templates in user dir

This commit is contained in:
Wancat
2022-11-17 17:03:40 +08:00
parent 3ef4dfaa48
commit fc4c269ddc
3 changed files with 16 additions and 11 deletions

View File

@@ -10,6 +10,7 @@ import (
"os/exec"
"path"
"strings"
"text/template"
"time"
)
@@ -140,9 +141,14 @@ type TxData struct {
Source string `form:"src"`
}
func newTx(data TxData) (result string, err error) {
func (u *User) newTx(data TxData) (result string, err error) {
data.Date = time.Now().Format("2006/01/02")
var buf bytes.Buffer
err = ledgerTpl.ExecuteTemplate(&buf, data.Action, data)
return buf.String(), nil
tpl, err := template.ParseFiles(u.FilePath(data.Action))
if err != nil {
return
}
err = tpl.ExecuteTemplate(&buf, data.Action, data)
result = buf.String()
return
}

View File

@@ -51,15 +51,16 @@ func router() *gin.Engine {
c.AbortWithError(400, err)
return
}
tx, err := newTx(data)
user := getUser(c)
tx, err := user.newTx(data)
if err != nil {
c.AbortWithError(400, err)
log.Println(err, c.Request.Form)
return
}
HTML(c, 200, "new.html", struct {
Tx string
}{tx})
HTML(c, 200, "new.html", gin.H{
"Tx": tx,
})
})
authZone.POST("/submit", func(c *gin.Context) {
user := getUser(c)
@@ -69,9 +70,7 @@ func router() *gin.Engine {
return
}
HTML(c, 200, "success.html", struct {
Tx string
}{tx})
c.Redirect(303, "/dashboard")
})
authZone.GET("/edit", func(c *gin.Context) {

View File

@@ -1,6 +1,6 @@
{{ define "title" }}Success{{ end }}
{{ define "main" }}
<p><strong>Success</strong></p>
<p><a href="/edit?filename={{ .FileName }}">繼續編輯</a></p>
{{ with .FileName }}<p><a href="/edit?filename={{ . }}">繼續編輯</a></p>{{ end }}
<pre><code>{{ .Tx }}</code></pre>
{{ end }}