diff --git a/controller/pager.go b/controller/pager.go index 90f5168..39710d2 100644 --- a/controller/pager.go +++ b/controller/pager.go @@ -61,18 +61,18 @@ func (p *Pager) SetItems(items int) { } // IsBeginning determines if the pager is at the beginning of the pages -func (p *Pager) IsBeginning() bool { +func (p Pager) IsBeginning() bool { return p.Page == 1 } // IsEnd determines if the pager is at the end of the pages -func (p *Pager) IsEnd() bool { +func (p Pager) IsEnd() bool { return p.Page >= p.Pages } // GetOffset determines the offset of the results in order to get the items for // the current page -func (p *Pager) GetOffset() int { +func (p Pager) GetOffset() int { if p.Page == 0 { p.Page = 1 } diff --git a/routes/home.go b/routes/home.go index deb718d..8cd9c9f 100644 --- a/routes/home.go +++ b/routes/home.go @@ -1,22 +1,46 @@ package routes import ( + "fmt" + "goweb/controller" "github.com/labstack/echo/v4" ) -type Home struct { - controller.Controller +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) } -func (h *Home) Get(c echo.Context) error { - p := controller.NewPage(c) - p.Layout = "main" - p.Name = "home" - p.Data = "Hello world" - p.Metatags.Description = "Welcome to the homepage." - p.Metatags.Keywords = []string{"Go", "MVC", "Web", "Software"} +// 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) - return h.RenderPage(c, p) + 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] } diff --git a/static/gopher.png b/static/gopher.png index 81ef2e0..0266be0 100644 Binary files a/static/gopher.png and b/static/gopher.png differ diff --git a/templates/pages/home.gohtml b/templates/pages/home.gohtml index dc4ec98..ba777c3 100644 --- a/templates/pages/home.gohtml +++ b/templates/pages/home.gohtml @@ -1,4 +1,51 @@ {{define "content"}} - Hello homepage {{upper "data"}}: {{ .Data }} -

Gopher

+ {{- if not (eq .HTMX.Request.Target "posts")}} + Hello homepage +

Gopher

+ +
+

Recent posts

+

+ Below is an example of both paging and AJAX fetching using HTMX +

+
+ {{- end}} + + {{template "posts" .}} +{{end}} + +{{define "posts"}} +
+ {{- range .Data}} +
+
+

+ Gopher +

+
+
+
+

+ {{.Title}} +
+ {{.Body}} +

+
+
+
+ {{- end}} + +
+ {{- if not $.Pager.IsBeginning}} +

+ +

+ {{- end}} + {{- if not $.Pager.IsEnd}} +

+ +

+ {{- end}} +
+
{{end}} \ No newline at end of file