saasitone/pkg/handlers/pages.go

94 lines
2.6 KiB
Go
Raw Permalink Normal View History

package handlers
2021-12-03 03:11:01 -08:00
import (
"github.com/labstack/echo/v4"
2024-07-09 17:57:05 -07:00
"git.grosinger.net/tgrosinger/saasitone/pkg/page"
"git.grosinger.net/tgrosinger/saasitone/pkg/services"
2024-07-14 18:48:40 -07:00
"git.grosinger.net/tgrosinger/saasitone/templ/layouts"
"git.grosinger.net/tgrosinger/saasitone/templ/pages"
)
const (
routeNameAbout = "about"
routeNameHome = "home"
2021-12-03 03:11:01 -08:00
)
type (
Pages struct {
*services.TemplateRenderer
*services.DBClient
}
)
2021-12-03 03:11:01 -08:00
func init() {
Register(new(Pages))
}
func (h *Pages) Init(c *services.Container) error {
h.TemplateRenderer = c.TemplateRenderer
h.DBClient = c.DB
return nil
}
func (h *Pages) Routes(g *echo.Group) {
g.GET("/", h.Home).Name = routeNameHome
g.GET("/about", h.About).Name = routeNameAbout
}
func (h *Pages) Home(ctx echo.Context) error {
p := page.New(ctx)
p.Metatags.Description = "Welcome to the homepage."
p.Metatags.Keywords = []string{"Go", "MVC", "Web", "Software"}
p.Pager = page.NewPager(ctx, 4)
p.LayoutComponent = layouts.Main
data := h.Post.FetchAll(&p.Pager)
component := pages.Home(p, data)
return h.RenderPageTempl(ctx, p, component)
}
func (h *Pages) About(ctx echo.Context) error {
p := page.New(ctx)
p.Title = "About"
p.LayoutComponent = layouts.Main
// This page will be cached!
p.Cache.Enabled = true
p.Cache.Tags = []string{"page_about", "page:list"}
2021-12-03 03:11:01 -08:00
// A simple example of how the Data field can contain anything you want to send to the templates
2021-12-25 11:20:49 -08:00
// even though you wouldn't normally send markup like this
2024-07-17 17:01:38 -07:00
data := pages.AboutData{
ShowCacheWarning: true,
2024-07-17 17:01:38 -07:00
FrontendTabs: []pages.AboutTab{
{
Title: "HTMX",
2024-07-17 17:01:38 -07:00
Body: `Completes HTML as a hypertext by providing attributes to AJAXify anything and much more. Visit <a href="https://htmx.org/">htmx.org</a> to learn more.`,
},
{
Title: "Alpine.js",
2024-07-17 17:01:38 -07:00
Body: `Drop-in, Vue-like functionality written directly in your markup. Visit <a href="https://alpinejs.dev/">alpinejs.dev</a> to learn more.`,
},
{
Title: "Bulma",
2024-07-17 17:01:38 -07:00
Body: `Ready-to-use frontend components that you can easily combine to build responsive web interfaces with no JavaScript requirements. Visit <a href="https://bulma.io/">bulma.io</a> to learn more.`,
},
},
2024-07-17 17:01:38 -07:00
BackendTabs: []pages.AboutTab{
2021-12-25 11:20:49 -08:00
{
Title: "Echo",
2024-07-17 17:01:38 -07:00
Body: `High performance, extensible, minimalist Go web framework. Visit <a href="https://echo.labstack.com/">echo.labstack.com</a> to learn more.`,
2021-12-25 11:20:49 -08:00
},
{
Title: "Ent",
2024-07-17 17:01:38 -07:00
Body: `Simple, yet powerful ORM for modeling and querying data. Visit <a href="https://entgo.io/">entgo.io</a> to learn more.`,
2021-12-25 11:20:49 -08:00
},
},
}
2024-07-17 17:01:38 -07:00
component := pages.About(p, data)
return h.RenderPageTempl(ctx, p, component)
2021-12-03 03:11:01 -08:00
}