https://github.com/timothyye/martini-paginate
A pagination plugin for go-martini web framework.
https://github.com/timothyye/martini-paginate
Last synced: 11 months ago
JSON representation
A pagination plugin for go-martini web framework.
- Host: GitHub
- URL: https://github.com/timothyye/martini-paginate
- Owner: TimothyYe
- License: mit
- Created: 2014-09-25T11:19:15.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-06-05T09:05:26.000Z (about 11 years ago)
- Last Synced: 2024-12-28T10:30:20.523Z (over 1 year ago)
- Language: Go
- Size: 160 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
martini-paginate
================
A pagination plugin for go-martini web framework.
## Installation
Import it into source code:
```go
import (
"github.com/timothyye/martini-paginate"
)
```
Then get package and source code:
```bash
go get
```
## Usage
* Set paginator's handler as the middleware:
```go
m = martini.Classic()
m.Get("/casts", paginate.Handler, HandleCasts)
```
* In handler function, initialize the paginator with page size and record total amount:
```go
func HandleCasts(r render.Render, db *mgo.Database, pager *paginate.Paginator) {
num, _ := db.C("casts").Count()
pers := 6
pager.Init(pers, num)
casts := []models.Casts{}
db.C("casts").Find(nil).Limit(pers).Skip(pager.Offset()).All(&casts)
r.HTML(200, "casts", map[string]interface{}{
"IsCasts": true,
"Casts": casts,
"Paginator": pager,
"Num": num})
}
```
* In template, visit the paginator and render the page:
```html
{{if gt .Paginator.PageNums 1}}
{{if .Paginator.HasPrev}}
- First
- <
{{else}}
- First
- <
{{end}}
{{range $index, $page := .Paginator.Pages}}
-
{{$page}}
{{end}}
{{if .Paginator.HasNext}}
- >
- Last
{{else}}
- >
- Last
{{end}}
{{end}}
```
## More examples
For more examples, please refer to the source code of [Vim-Tips.com](https://github.com/TimothyYe/vim-tips-web)