Static paging refers to the use of route parameters for page pagination, where the paging object is highly coupled with the route definition of the Server. The route definition requires a route parameter with the name page, which can use fuzzy matching route *page, named matching route :page, or field matching route {page}.

Example 1, Using Fuzzy Matching Route

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. "github.com/gogf/gf/v2/os/gview"
  6. )
  7. func main() {
  8. s := g.Server()
  9. s.BindHandler("/page/static/*page", func(r *ghttp.Request) {
  10. page := r.GetPage(100, 10)
  11. buffer, _ := gview.ParseContent(`
  12. <html>
  13. <head>
  14. <style>
  15. a,span {padding:8px; font-size:16px;}
  16. div{margin:5px 5px 20px 5px}
  17. </style>
  18. </head>
  19. <body>
  20. <div>{{.page1}}</div>
  21. <div>{{.page2}}</div>
  22. <div>{{.page3}}</div>
  23. <div>{{.page4}}</div>
  24. </body>
  25. </html>
  26. `, g.Map{
  27. "page1": page.GetContent(1),
  28. "page2": page.GetContent(2),
  29. "page3": page.GetContent(3),
  30. "page4": page.GetContent(4),
  31. })
  32. r.Response.Write(buffer)
  33. })
  34. s.SetPort(8199)
  35. s.Run()
  36. }

After execution, we manually visit the page http://127.0.0.1:8199/page/static/6, and the result is as follows:

Pagination - Static Paging - 图1

Example 2, Using Field Matching Route

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. "github.com/gogf/gf/v2/os/gview"
  6. )
  7. func main() {
  8. s := g.Server()
  9. s.BindHandler("/:obj/*action/{page}.html", func(r *ghttp.Request) {
  10. page := r.GetPage(100, 10)
  11. buffer, _ := gview.ParseContent(`
  12. <html>
  13. <head>
  14. <style>
  15. a,span {padding:8px; font-size:16px;}
  16. div{margin:5px 5px 20px 5px}
  17. </style>
  18. </head>
  19. <body>
  20. <div>{{.page1}}</div>
  21. <div>{{.page2}}</div>
  22. <div>{{.page3}}</div>
  23. <div>{{.page4}}</div>
  24. </body>
  25. </html>
  26. `, g.Map{
  27. "page1": page.GetContent(1),
  28. "page2": page.GetContent(2),
  29. "page3": page.GetContent(3),
  30. "page4": page.GetContent(4),
  31. })
  32. r.Response.Write(buffer)
  33. })
  34. s.SetPort(8199)
  35. s.Run()
  36. }

The routing rule in this example is more flexible, using the {page} field matching rule to obtain current page number information. After execution, we visit any URL according to the routing rule, such as: http://127.0.0.1:8199/order/list/6.html, and the result is as shown below:

Pagination - Static Paging - 图2