Introduce GlobalRenderData and RenderData
GlobalRenderData contains some global metadata that can be obtained from any template. RenderData is a base type for template data. It contains a Global field with global metadata and an Extra field for plugins.
This commit is contained in:
parent
edf738f23d
commit
3748b4413e
2 changed files with 40 additions and 7 deletions
15
handlers.go
15
handlers.go
|
@ -17,11 +17,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type MailboxRenderData struct {
|
type MailboxRenderData struct {
|
||||||
|
RenderData
|
||||||
Mailbox *imap.MailboxStatus
|
Mailbox *imap.MailboxStatus
|
||||||
Mailboxes []*imap.MailboxInfo
|
Mailboxes []*imap.MailboxInfo
|
||||||
Messages []imapMessage
|
Messages []imapMessage
|
||||||
PrevPage, NextPage int
|
PrevPage, NextPage int
|
||||||
Extra map[string]interface{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleGetMailbox(ectx echo.Context) error {
|
func handleGetMailbox(ectx echo.Context) error {
|
||||||
|
@ -67,17 +67,18 @@ func handleGetMailbox(ectx echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.Render(http.StatusOK, "mailbox.html", &MailboxRenderData{
|
return ctx.Render(http.StatusOK, "mailbox.html", &MailboxRenderData{
|
||||||
|
RenderData: *NewRenderData(ctx),
|
||||||
Mailbox: mbox,
|
Mailbox: mbox,
|
||||||
Mailboxes: mailboxes,
|
Mailboxes: mailboxes,
|
||||||
Messages: msgs,
|
Messages: msgs,
|
||||||
PrevPage: prevPage,
|
PrevPage: prevPage,
|
||||||
NextPage: nextPage,
|
NextPage: nextPage,
|
||||||
Extra: make(map[string]interface{}),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleLogin(ectx echo.Context) error {
|
func handleLogin(ectx echo.Context) error {
|
||||||
ctx := ectx.(*context)
|
ctx := ectx.(*context)
|
||||||
|
|
||||||
username := ctx.FormValue("username")
|
username := ctx.FormValue("username")
|
||||||
password := ctx.FormValue("password")
|
password := ctx.FormValue("password")
|
||||||
if username != "" && password != "" {
|
if username != "" && password != "" {
|
||||||
|
@ -93,7 +94,7 @@ func handleLogin(ectx echo.Context) error {
|
||||||
return ctx.Redirect(http.StatusFound, "/mailbox/INBOX")
|
return ctx.Redirect(http.StatusFound, "/mailbox/INBOX")
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.Render(http.StatusOK, "login.html", nil)
|
return ctx.Render(http.StatusOK, "login.html", NewRenderData(ctx))
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleLogout(ectx echo.Context) error {
|
func handleLogout(ectx echo.Context) error {
|
||||||
|
@ -105,12 +106,12 @@ func handleLogout(ectx echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
type MessageRenderData struct {
|
type MessageRenderData struct {
|
||||||
|
RenderData
|
||||||
Mailbox *imap.MailboxStatus
|
Mailbox *imap.MailboxStatus
|
||||||
Message *imapMessage
|
Message *imapMessage
|
||||||
Body string
|
Body string
|
||||||
PartPath string
|
PartPath string
|
||||||
MailboxPage int
|
MailboxPage int
|
||||||
Extra map[string]interface{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleGetPart(ctx *context, raw bool) error {
|
func handleGetPart(ctx *context, raw bool) error {
|
||||||
|
@ -172,18 +173,18 @@ func handleGetPart(ctx *context, raw bool) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.Render(http.StatusOK, "message.html", &MessageRenderData{
|
return ctx.Render(http.StatusOK, "message.html", &MessageRenderData{
|
||||||
|
RenderData: *NewRenderData(ctx),
|
||||||
Mailbox: mbox,
|
Mailbox: mbox,
|
||||||
Message: msg,
|
Message: msg,
|
||||||
Body: body,
|
Body: body,
|
||||||
PartPath: partPathString,
|
PartPath: partPathString,
|
||||||
MailboxPage: int(mbox.Messages-msg.SeqNum) / messagesPerPage,
|
MailboxPage: int(mbox.Messages-msg.SeqNum) / messagesPerPage,
|
||||||
Extra: make(map[string]interface{}),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type ComposeRenderData struct {
|
type ComposeRenderData struct {
|
||||||
|
RenderData
|
||||||
Message *OutgoingMessage
|
Message *OutgoingMessage
|
||||||
Extra map[string]interface{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleCompose(ectx echo.Context) error {
|
func handleCompose(ectx echo.Context) error {
|
||||||
|
@ -281,7 +282,7 @@ func handleCompose(ectx echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.Render(http.StatusOK, "compose.html", &ComposeRenderData{
|
return ctx.Render(http.StatusOK, "compose.html", &ComposeRenderData{
|
||||||
|
RenderData: *NewRenderData(ctx),
|
||||||
Message: &msg,
|
Message: &msg,
|
||||||
Extra: make(map[string]interface{}),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
32
template.go
32
template.go
|
@ -13,6 +13,38 @@ import (
|
||||||
|
|
||||||
const themesDir = "public/themes"
|
const themesDir = "public/themes"
|
||||||
|
|
||||||
|
// GlobalRenderData contains data available in all templates.
|
||||||
|
type GlobalRenderData struct {
|
||||||
|
LoggedIn bool
|
||||||
|
|
||||||
|
// if logged in
|
||||||
|
Username string
|
||||||
|
// TODO: list of mailboxes
|
||||||
|
|
||||||
|
Extra map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RenderData is the base type for templates. It should be extended with new
|
||||||
|
// template-specific fields.
|
||||||
|
type RenderData struct {
|
||||||
|
Global GlobalRenderData
|
||||||
|
Extra map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRenderData(ctx *context) *RenderData {
|
||||||
|
global := GlobalRenderData{Extra: make(map[string]interface{})}
|
||||||
|
|
||||||
|
if ctx.session != nil {
|
||||||
|
global.LoggedIn = true
|
||||||
|
global.Username = ctx.session.username
|
||||||
|
}
|
||||||
|
|
||||||
|
return &RenderData{
|
||||||
|
Global: global,
|
||||||
|
Extra: make(map[string]interface{}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type renderer struct {
|
type renderer struct {
|
||||||
base *template.Template
|
base *template.Template
|
||||||
themes map[string]*template.Template
|
themes map[string]*template.Template
|
||||||
|
|
Loading…
Reference in a new issue