lishwist/http/templates/templates.go

64 lines
1.4 KiB
Go

package templates
import (
"fmt"
"html/template"
"lishwist/http/env"
"github.com/Teajey/hmc"
)
type NavUser struct {
Label string `json:"label" xml:",chardata"`
Alert bool `json:"alert,omitempty" xml:",omitempty"`
Link *hmc.Link `json:"link,omitempty" xml:",omitempty"`
Account *hmc.Link `json:"account,omitempty" xml:",omitempty"`
Logout hmc.Form[hmc.Input] `json:"logout"`
}
type NavCollapse struct {
Links []hmc.Link
User *NavUser `json:",omitempty"`
}
func DefaultNavCollapse() NavCollapse {
return NavCollapse{Links: []hmc.Link{{Href: "/", Label: "Home"}}}
}
func UserNavCollapse(username, reference string, accountAlert bool) NavCollapse {
return NavCollapse{
Links: []hmc.Link{{Href: "/", Label: "Home"}},
User: &NavUser{
Label: fmt.Sprintf("Logged in as '%s'", username),
Alert: accountAlert,
Link: &hmc.Link{
Label: "Share link",
Href: fmt.Sprintf("%s/lists/%s", env.Configuration.HostUrl, reference),
},
Account: &hmc.Link{
Label: "Account",
Href: "/account",
},
Logout: hmc.Form[hmc.Input]{
Method: "POST",
Action: "/logout",
Elements: hmc.Input{
Label: "Logout",
Type: "submit",
},
},
},
}
}
var Template *template.Template
func init() {
Template = load()
}
func load() *template.Template {
t := template.Must(template.ParseGlob("templates/*.gotmpl"))
return t
}