mirror of
https://github.com/gohugoio/hugo.git
synced 2024-06-30 21:39:07 +00:00
Improve error message when attempting to paginate from a single page template
Fixes #11953
This commit is contained in:
parent
4d98b0ed6a
commit
6c3b6ba3e6
|
@ -14,6 +14,8 @@
|
||||||
package hugolib
|
package hugolib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/identity"
|
"github.com/gohugoio/hugo/identity"
|
||||||
"github.com/gohugoio/hugo/output"
|
"github.com/gohugoio/hugo/output"
|
||||||
"github.com/gohugoio/hugo/resources/page"
|
"github.com/gohugoio/hugo/resources/page"
|
||||||
|
@ -37,12 +39,16 @@ func newPageOutput(
|
||||||
targetPathsProvider = ft
|
targetPathsProvider = ft
|
||||||
linksProvider = ft
|
linksProvider = ft
|
||||||
|
|
||||||
var paginatorProvider page.PaginatorProvider = page.NopPage
|
var paginatorProvider page.PaginatorProvider
|
||||||
var pag *pagePaginator
|
var pag *pagePaginator
|
||||||
|
|
||||||
if render && ps.IsNode() {
|
if render && ps.IsNode() {
|
||||||
pag = newPagePaginator(ps)
|
pag = newPagePaginator(ps)
|
||||||
paginatorProvider = pag
|
paginatorProvider = pag
|
||||||
|
} else {
|
||||||
|
paginatorProvider = page.PaginatorNotSupportedFunc(func() error {
|
||||||
|
return fmt.Errorf("pagination not supported for pages of kind %q", ps.Kind())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var dependencyManager identity.Manager = identity.NopManager
|
var dependencyManager identity.Manager = identity.NopManager
|
||||||
|
|
|
@ -157,3 +157,16 @@ Len Pag: {{ len $pag.Pages }}
|
||||||
|
|
||||||
b.AssertFileContent("public/index.html", "Len: 0", "Len Pag: 0")
|
b.AssertFileContent("public/index.html", "Len: 0", "Len Pag: 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPaginatorNodePagesOnly(t *testing.T) {
|
||||||
|
files := `
|
||||||
|
-- hugo.toml --
|
||||||
|
paginate = 1
|
||||||
|
-- content/p1.md --
|
||||||
|
-- layouts/_default/single.html --
|
||||||
|
Paginator: {{ .Paginator }}
|
||||||
|
`
|
||||||
|
b, err := TestE(t, files)
|
||||||
|
b.Assert(err, qt.IsNotNil)
|
||||||
|
b.Assert(err.Error(), qt.Contains, `error calling Paginator: pagination not supported for pages of kind "page"`)
|
||||||
|
}
|
||||||
|
|
|
@ -32,6 +32,18 @@ type PaginatorProvider interface {
|
||||||
Paginate(pages any, options ...any) (*Pager, error)
|
Paginate(pages any, options ...any) (*Pager, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ PaginatorProvider = (*PaginatorNotSupportedFunc)(nil)
|
||||||
|
|
||||||
|
type PaginatorNotSupportedFunc func() error
|
||||||
|
|
||||||
|
func (f PaginatorNotSupportedFunc) Paginate(pages any, options ...any) (*Pager, error) {
|
||||||
|
return nil, f()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f PaginatorNotSupportedFunc) Paginator(options ...any) (*Pager, error) {
|
||||||
|
return nil, f()
|
||||||
|
}
|
||||||
|
|
||||||
// Pager represents one of the elements in a paginator.
|
// Pager represents one of the elements in a paginator.
|
||||||
// The number, starting on 1, represents its place.
|
// The number, starting on 1, represents its place.
|
||||||
type Pager struct {
|
type Pager struct {
|
||||||
|
|
Loading…
Reference in a new issue