Beispiel #1
0
func TestPager(t *testing.T) {
	cursor := helper.NewPagerCursor(5, 99)
	Convey("test pager", t, func() {
		Convey("test all pager", func() {
			var i = 1
			for i = 1; i < 100; i++ {
				if p := cursor.Page(i); p == nil {
					break
				}
			}
			So(i-1, ShouldEqual, 20)

			p := cursor.Page(-1)
			So(p, ShouldBeNil)
		})

		Convey("each pager", func() {
			p := cursor.Page(1)
			So(p.Prev, ShouldEqual, 0)
			So(p.PrevUrl(), ShouldBeEmpty)

			p = cursor.Page(20)
			So(p.Prev, ShouldEqual, 19)
			So(p.Next, ShouldEqual, 0)
			So(p.End, ShouldEqual, 99)
			So(p.NextUrl(), ShouldBeEmpty)

			p = cursor.Page(10)
			p.SetLayout("page%d")
			So(p.PrevUrl(), ShouldEqual, "page9")
			So(p.NextUrl(), ShouldEqual, "page11")
		})
	})
}
Beispiel #2
0
// compile paged posts to html
func (b *Builder) compilePagedPost(ctx *Context) {
	// post pagination
	var (
		currentPosts []*model.Post = nil
		cursor                     = helper.NewPagerCursor(4, len(ctx.Posts))
		page         int           = 1
		layout                     = "posts/%d"
	)
	for {
		pager := cursor.Page(page)
		if pager == nil {
			ctx.PostPageCount = page - 1
			break
		}

		currentPosts = ctx.Posts[pager.Begin:pager.End]
		pager.SetLayout("/" + layout + ".html")

		dstFile := path.Join(ctx.DstDir, fmt.Sprintf(layout+".html", pager.Page))

		viewData := ctx.ViewData()
		viewData["Title"] = fmt.Sprintf("Page %d - %s", pager.Page, ctx.Meta.Title)
		viewData["Posts"] = currentPosts
		viewData["Pager"] = pager

		if err := b.compileTemplate(ctx, "posts.html", viewData, dstFile); err != nil {
			ctx.Error = err
			return
		}

		if page == 1 {
			ctx.indexPosts = currentPosts
			ctx.indexPager = pager
		}
		page++
	}
	log15.Debug("Post.Pages." + strconv.Itoa(page-1))
}