saasitone/controllers/page.go

55 lines
951 B
Go
Raw Normal View History

2021-12-03 03:11:01 -08:00
package controllers
import (
"html/template"
"net/http"
"goweb/msg"
"github.com/labstack/echo/v4"
)
type Page struct {
AppName string
Title string
Context echo.Context
Reverse func(name string, params ...interface{}) string
Path string
Data interface{}
Layout string
Name string
IsHome bool
IsAuth bool
StatusCode int
Metatags struct {
Description string
Keywords []string
}
}
func NewPage(c echo.Context) Page {
p := Page{
Context: c,
Reverse: c.Echo().Reverse,
Path: c.Request().URL.Path,
StatusCode: http.StatusOK,
}
p.IsHome = p.Path == "/"
return p
}
func (p Page) SetMessage(typ msg.Type, value string) {
msg.Set(p.Context, typ, value)
}
func (p Page) GetMessages(typ msg.Type) []template.HTML {
strs := msg.Get(p.Context, typ)
ret := make([]template.HTML, len(strs))
for k, v := range strs {
ret[k] = template.HTML(v)
}
return ret
}