An open API service indexing awesome lists of open source software.

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.

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)