Skip to content

Commit

Permalink
Makes Paginate accept slices of PageGroup (in addition to PagesGroup)
Browse files Browse the repository at this point in the history
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 }}
  • Loading branch information
Vincent Danjean authored and vdanjean committed Aug 31, 2018
1 parent ace1603 commit afee047
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion hugolib/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -414,6 +418,36 @@ func paginatePages(td targetPathDescriptor, seq interface{}, pagerSize int) (pag
return pagers, nil
}

func toPagesGroup(seq interface{}) (PagesGroup, error) {
switch v := seq.(type) {
case nil:
return nil, nil
case PagesGroup:
return v, nil
case []PageGroup:
return PagesGroup(v), nil
case []interface{}:
l := len(v)
if l == 0 {
break
}
switch v[0].(type) {
case PageGroup:
pagesGroup := make(PagesGroup, l)
for i, ipg := range v {
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
Expand Down

0 comments on commit afee047

Please sign in to comment.