saasitone/routes/home.go

47 lines
1.0 KiB
Go
Raw Normal View History

2021-12-14 08:13:53 -08:00
package routes
2021-12-03 03:11:01 -08:00
import (
2021-12-24 14:58:53 -08:00
"fmt"
2022-01-01 07:44:18 -08:00
"github.com/mikestefanello/pagoda/controller"
2021-12-03 03:11:01 -08:00
"github.com/labstack/echo/v4"
)
2021-12-24 14:58:53 -08:00
type (
Home struct {
controller.Controller
}
Post struct {
Title string
Body string
}
)
func (c *Home) Get(ctx echo.Context) error {
page := controller.NewPage(ctx)
page.Layout = "main"
page.Name = "home"
page.Metatags.Description = "Welcome to the homepage."
page.Metatags.Keywords = []string{"Go", "MVC", "Web", "Software"}
page.Pager = controller.NewPager(ctx, 4)
page.Data = c.fetchPosts(&page.Pager)
return c.RenderPage(ctx, page)
2021-12-03 03:11:01 -08:00
}
2021-12-24 14:58:53 -08:00
// fetchPosts is an mock example of fetching posts to illustrate how paging works
func (c *Home) fetchPosts(pager *controller.Pager) []Post {
pager.SetItems(20)
posts := make([]Post, 20)
2021-12-03 03:11:01 -08:00
2021-12-24 14:58:53 -08:00
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]
2021-12-03 03:11:01 -08:00
}