From 1fa0cc54a4fb9128551f7e915674f11b9bf40327 Mon Sep 17 00:00:00 2001 From: Vincent Danjean Date: Tue, 28 Aug 2018 22:08:55 +0200 Subject: [PATCH] Makes Paginate accept slices of PageGroup (in addition to PagesGroup) User can now do something such as: {{ $cool := .Page.Group "cool" (where .Site.RegularPages "Params.cool" true) }} {{ $blue := .Page.Group "blue" (where .Site.RegularPages "Params.blue" true) }} {{ $paginator := .Page.Paginate (slice $cool $blue) }} {{ range $paginator.PageGroups }} {{ markdownify (printf "## KEY: %s" .Key) }} {{ range .Pages }} {{ partial "lists/partials/blogentry.html" (dict "Page" .) }} {{ end }} {{ end }} --- hugolib/pagination.go | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/hugolib/pagination.go b/hugolib/pagination.go index b3cd7dac805..b1d6be8adac 100644 --- a/hugolib/pagination.go +++ b/hugolib/pagination.go @@ -399,7 +399,11 @@ func paginatePages(td targetPathDescriptor, seq interface{}, pagerSize int) (pag var paginator *paginator - if groups, ok := seq.(PagesGroup); ok { + groups, err := toPagesGroup(seq) + if err != nil { + return nil, err + } + if groups != nil { paginator, _ = newPaginatorFromPageGroups(groups, pagerSize, urlFactory) } else { pages, err := toPages(seq) @@ -414,6 +418,39 @@ func paginatePages(td targetPathDescriptor, seq interface{}, pagerSize int) (pag return pagers, nil } +func toPagesGroup(seq interface{}) (PagesGroup, error) { + if seq == nil { + return nil, nil + } + + switch seq.(type) { + case PagesGroup: + return seq.(PagesGroup), nil + case []PageGroup: + return PagesGroup(seq.([]PageGroup)), nil + case []interface{}: + list := seq.([]interface{}) + l := len(list) + if l == 0 { + break + } + switch list[0].(type) { + case PageGroup: + pagesGroup := make([]PageGroup, len(list)) + for i, ipg := range list { + if pg, ok := ipg.(PageGroup); ok { + pagesGroup[i] = pg + } else { + return nil, fmt.Errorf("unsupported type in paginate from slice, got %T instead of PageGroup", ipg) + } + } + return PagesGroup(pagesGroup), nil + } + } + + return nil, nil +} + func toPages(seq interface{}) (Pages, error) { if seq == nil { return Pages{}, nil