Paging #4
|
|
@ -9,3 +9,9 @@
|
||||||
{{ template "_post_meta.html" . }}
|
{{ template "_post_meta.html" . }}
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
{{ if or .PrevURL .NextURL }}
|
||||||
|
<nav class="pagination">
|
||||||
|
{{ if .PrevURL }}<a href="{{ .PrevURL }}">← Newer posts</a>{{ end }}
|
||||||
|
{{ if .NextURL }}<a href="{{ .NextURL }}">Older posts →</a>{{ end }}
|
||||||
|
</nav>
|
||||||
|
{{ end }}
|
||||||
|
|
|
||||||
|
|
@ -372,7 +372,8 @@ func (b *Builder) renderCategoryPages(ctx buildContext, goCtx context.Context) e
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var posts []postSingleData
|
// Collect all posts for this category
|
||||||
|
var allPosts []postSingleData
|
||||||
for mp := range b.site.PostIterByCategory(goCtx, cwc.ID) {
|
for mp := range b.site.PostIterByCategory(goCtx, cwc.ID) {
|
||||||
post, err := mp.Get()
|
post, err := mp.Get()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -382,7 +383,7 @@ func (b *Builder) renderCategoryPages(ctx buildContext, goCtx context.Context) e
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
posts = append(posts, rp)
|
allPosts = append(allPosts, rp)
|
||||||
}
|
}
|
||||||
|
|
||||||
var descHTML bytes.Buffer
|
var descHTML bytes.Buffer
|
||||||
|
|
@ -392,22 +393,68 @@ func (b *Builder) renderCategoryPages(ctx buildContext, goCtx context.Context) e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
postsPerPage := b.site.PostsPerPage
|
||||||
|
if postsPerPage < 1 {
|
||||||
|
postsPerPage = 10
|
||||||
|
}
|
||||||
|
|
||||||
|
totalPages := (len(allPosts) + postsPerPage - 1) / postsPerPage
|
||||||
|
if totalPages < 1 {
|
||||||
|
totalPages = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
basePath := fmt.Sprintf("/categories/%s", cwc.Slug)
|
||||||
|
|
||||||
|
for page := 1; page <= totalPages; page++ {
|
||||||
|
start := (page - 1) * postsPerPage
|
||||||
|
end := start + postsPerPage
|
||||||
|
if end > len(allPosts) {
|
||||||
|
end = len(allPosts)
|
||||||
|
}
|
||||||
|
|
||||||
|
pageInfo := models.PageInfo{
|
||||||
|
CurrentPage: page,
|
||||||
|
TotalPages: totalPages,
|
||||||
|
PostsPerPage: postsPerPage,
|
||||||
|
}
|
||||||
|
|
||||||
|
var prevURL, nextURL string
|
||||||
|
if page > 1 {
|
||||||
|
if page == 2 {
|
||||||
|
prevURL = basePath + "/"
|
||||||
|
} else {
|
||||||
|
prevURL = fmt.Sprintf("%s/page/%d/", basePath, page-1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if page < totalPages {
|
||||||
|
nextURL = fmt.Sprintf("%s/page/%d/", basePath, page+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
path := basePath
|
||||||
|
if page > 1 {
|
||||||
|
path = fmt.Sprintf("%s/page/%d", basePath, page)
|
||||||
|
}
|
||||||
|
|
||||||
data := categorySingleData{
|
data := categorySingleData{
|
||||||
commonData: commonData{Site: b.site},
|
commonData: commonData{Site: b.site},
|
||||||
Category: &cwc.Category,
|
Category: &cwc.Category,
|
||||||
DescriptionHTML: template.HTML(descHTML.String()),
|
DescriptionHTML: template.HTML(descHTML.String()),
|
||||||
Posts: posts,
|
Posts: allPosts[start:end],
|
||||||
Path: fmt.Sprintf("/categories/%s", cwc.Slug),
|
Path: path,
|
||||||
|
PageInfo: pageInfo,
|
||||||
|
PrevURL: prevURL,
|
||||||
|
NextURL: nextURL,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := b.createAtPath(ctx, data.Path, func(f io.Writer) error {
|
if err := b.createAtPath(ctx, path, func(f io.Writer) error {
|
||||||
return b.renderTemplate(f, tmplNameCategorySingle, data)
|
return b.renderTemplate(f, tmplNameCategorySingle, data)
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Per-category feeds
|
// Per-category feeds (use all posts, not paginated)
|
||||||
if err := b.renderCategoryFeed(ctx, cwc, posts); err != nil {
|
if err := b.renderCategoryFeed(ctx, cwc, allPosts); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -88,4 +88,7 @@ type categorySingleData struct {
|
||||||
DescriptionHTML template.HTML
|
DescriptionHTML template.HTML
|
||||||
Posts []postSingleData
|
Posts []postSingleData
|
||||||
Path string
|
Path string
|
||||||
|
PageInfo models.PageInfo
|
||||||
|
PrevURL string
|
||||||
|
NextURL string
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue