package handlers import ( "github.com/a-h/templ" "github.com/labstack/echo/v4" "git.grosinger.net/tgrosinger/saasitone/pkg/page" "git.grosinger.net/tgrosinger/saasitone/pkg/services" "git.grosinger.net/tgrosinger/saasitone/templ/layouts" "git.grosinger.net/tgrosinger/saasitone/templ/pages" ) const ( routeNameAbout = "about" routeNameHome = "home" ) type ( Pages struct { *services.TemplateRenderer *services.DBClient } ) 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) data := h.Post.FetchAll(&p.Pager) component := pages.Home(p, data) // TODO: This can be reused p.LayoutComponent = func(content templ.Component) templ.Component { return layouts.Main(p, content) } return h.RenderPageTempl(ctx, p, component) } func (h *Pages) About(ctx echo.Context) error { p := page.New(ctx) p.Title = "About" // This page will be cached! p.Cache.Enabled = true p.Cache.Tags = []string{"page_about", "page:list"} // A simple example of how the Data field can contain anything you want to send to the templates // even though you wouldn't normally send markup like this data := pages.AboutData{ ShowCacheWarning: true, FrontendTabs: []pages.AboutTab{ { Title: "HTMX", Body: `Completes HTML as a hypertext by providing attributes to AJAXify anything and much more. Visit htmx.org to learn more.`, }, { Title: "Alpine.js", Body: `Drop-in, Vue-like functionality written directly in your markup. Visit alpinejs.dev to learn more.`, }, { Title: "Bulma", Body: `Ready-to-use frontend components that you can easily combine to build responsive web interfaces with no JavaScript requirements. Visit bulma.io to learn more.`, }, }, BackendTabs: []pages.AboutTab{ { Title: "Echo", Body: `High performance, extensible, minimalist Go web framework. Visit echo.labstack.com to learn more.`, }, { Title: "Ent", Body: `Simple, yet powerful ORM for modeling and querying data. Visit entgo.io to learn more.`, }, }, } component := pages.About(p, data) // TODO: This can be reused p.LayoutComponent = func(content templ.Component) templ.Component { return layouts.Main(p, content) } return h.RenderPageTempl(ctx, p, component) }