2024-06-09 09:31:30 -07:00
package handlers
2021-12-03 03:11:01 -08:00
import (
2024-06-09 09:31:30 -07:00
"fmt"
2021-12-25 09:35:13 -08:00
"html/template"
2024-06-09 09:31:30 -07:00
"github.com/labstack/echo/v4"
2024-06-15 12:34:24 -07:00
"github.com/mikestefanello/pagoda/pkg/page"
2024-06-09 09:31:30 -07:00
"github.com/mikestefanello/pagoda/pkg/services"
2023-12-16 08:07:20 -08:00
"github.com/mikestefanello/pagoda/templates"
2024-06-09 09:31:30 -07:00
)
2021-12-12 19:09:13 -08:00
2024-06-09 09:31:30 -07:00
const (
routeNameAbout = "about"
routeNameHome = "home"
2021-12-03 03:11:01 -08:00
)
2021-12-25 09:35:13 -08:00
type (
2024-06-09 09:31:30 -07:00
Pages struct {
2024-06-15 12:34:24 -07:00
* services . TemplateRenderer
2021-12-25 09:35:13 -08:00
}
2024-06-09 09:31:30 -07:00
post struct {
Title string
Body string
}
2022-02-10 05:56:07 -08:00
aboutData struct {
2021-12-25 09:35:13 -08:00
ShowCacheWarning bool
2022-02-10 05:56:07 -08:00
FrontendTabs [ ] aboutTab
BackendTabs [ ] aboutTab
2021-12-25 09:35:13 -08:00
}
2022-02-10 05:56:07 -08:00
aboutTab struct {
2021-12-25 09:35:13 -08:00
Title string
Body template . HTML
}
)
2021-12-03 03:11:01 -08:00
2024-06-09 09:31:30 -07:00
func init ( ) {
Register ( new ( Pages ) )
}
2024-06-15 12:34:24 -07:00
func ( h * Pages ) Init ( c * services . Container ) error {
h . TemplateRenderer = c . TemplateRenderer
2024-06-09 09:31:30 -07:00
return nil
}
2024-06-15 12:34:24 -07:00
func ( h * Pages ) Routes ( g * echo . Group ) {
g . GET ( "/" , h . Home ) . Name = routeNameHome
g . GET ( "/about" , h . About ) . Name = routeNameAbout
2024-06-09 09:31:30 -07:00
}
2024-06-15 12:34:24 -07:00
func ( h * Pages ) Home ( ctx echo . Context ) error {
p := page . New ( ctx )
p . Layout = templates . LayoutMain
p . Name = templates . PageHome
p . Metatags . Description = "Welcome to the homepage."
p . Metatags . Keywords = [ ] string { "Go" , "MVC" , "Web" , "Software" }
p . Pager = page . NewPager ( ctx , 4 )
p . Data = h . fetchPosts ( & p . Pager )
2024-06-09 09:31:30 -07:00
2024-06-15 12:34:24 -07:00
return h . RenderPage ( ctx , p )
2024-06-09 09:31:30 -07:00
}
// fetchPosts is an mock example of fetching posts to illustrate how paging works
2024-06-15 12:34:24 -07:00
func ( h * Pages ) fetchPosts ( pager * page . Pager ) [ ] post {
2024-06-09 09:31:30 -07:00
pager . SetItems ( 20 )
posts := make ( [ ] post , 20 )
for k := range posts {
posts [ k ] = post {
Title : fmt . Sprintf ( "Post example #%d" , k + 1 ) ,
Body : fmt . Sprintf ( "Lorem ipsum example #%d ddolor sit amet, consectetur adipiscing elit. Nam elementum vulputate tristique." , k + 1 ) ,
}
}
return posts [ pager . GetOffset ( ) : pager . GetOffset ( ) + pager . ItemsPerPage ]
}
2024-06-15 12:34:24 -07:00
func ( h * Pages ) About ( ctx echo . Context ) error {
p := page . New ( ctx )
p . Layout = templates . LayoutMain
p . Name = templates . PageAbout
p . Title = "About"
2021-12-25 09:35:13 -08:00
// This page will be cached!
2024-06-15 12:34:24 -07:00
p . Cache . Enabled = true
p . Cache . Tags = [ ] string { "page_about" , "page:list" }
2021-12-03 03:11:01 -08:00
2021-12-25 09:35:13 -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-06-15 12:34:24 -07:00
p . Data = aboutData {
2021-12-25 09:35:13 -08:00
ShowCacheWarning : true ,
2022-02-10 05:56:07 -08:00
FrontendTabs : [ ] aboutTab {
2021-12-25 09:35:13 -08:00
{
Title : "HTMX" ,
Body : template . HTML ( ` 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" ,
Body : template . HTML ( ` Drop-in, Vue-like functionality written directly in your markup. Visit <a href="https://alpinejs.dev/">alpinejs.dev</a> to learn more. ` ) ,
} ,
{
Title : "Bulma" ,
Body : template . HTML ( ` 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. ` ) ,
} ,
} ,
2022-02-10 05:56:07 -08:00
BackendTabs : [ ] aboutTab {
2021-12-25 11:20:49 -08:00
{
Title : "Echo" ,
Body : template . HTML ( ` High performance, extensible, minimalist Go web framework. Visit <a href="https://echo.labstack.com/">echo.labstack.com</a> to learn more. ` ) ,
} ,
{
Title : "Ent" ,
Body : template . HTML ( ` Simple, yet powerful ORM for modeling and querying data. Visit <a href="https://entgo.io/">entgo.io</a> to learn more. ` ) ,
} ,
} ,
2021-12-25 09:35:13 -08:00
}
2024-06-15 12:34:24 -07:00
return h . RenderPage ( ctx , p )
2021-12-03 03:11:01 -08:00
}