{"id":"23ddfb467b2a5b56","repo":"gohugoio/hugo","slug":"cannot-convert-type-t-to-pages","errorCode":null,"errorMessage":"cannot convert type %T to Pages","messagePattern":"cannot convert type %T to Pages","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"resources/page/pages.go","lineNumber":87,"sourceCode":"\t\tcopy(pages, v)\n\t\treturn pages, nil\n\tcase []any:\n\t\tpages := make(Pages, len(v))\n\t\tsuccess := true\n\t\tfor i, vv := range v {\n\t\t\tp, ok := vv.(Page)\n\t\t\tif !ok {\n\t\t\t\tsuccess = false\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tpages[i] = p\n\t\t}\n\t\tif success {\n\t\t\treturn pages, nil\n\t\t}\n\t}\n\n\treturn nil, fmt.Errorf(\"cannot convert type %T to Pages\", seq)\n}\n\n// Group groups the pages in in by key.\n// This implements collections.Grouper.\nfunc (p Pages) Group(key any, in any) (any, error) {\n\tpages, err := ToPages(in)\n\tif err != nil {\n\t\treturn PageGroup{}, err\n\t}\n\treturn PageGroup{Key: key, Pages: pages}, nil\n}\n\n// Len returns the number of pages in the list.\nfunc (p Pages) Len() int {\n\treturn len(p)\n}\n\n// ProbablyEq wraps compare.ProbablyEqer","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/resources/page/pages.go#L69-L105","documentation":"ToPages normalizes template values into a page.Pages collection, accepting Pages, *Pages, WeightedPages, PageGroup, []Page, and []any containing only Pages. Anything else — or an []any with a non-Page element — falls through to this error. It is commonly surfaced by functions that operate on page collections, such as .Group, sorting, and pagination helpers.","triggerScenarios":"Passing a non-page collection to a function that calls ToPages: `.Paginate` on a slice of strings/maps, `group` on a heterogeneous `slice` result, or handing PagesGroup (the whole grouped set) where a single group's .Pages was expected.","commonSituations":"Paginating `where .Site.Data ...` results or other data-file slices (data is not Pages); building a mixed `slice` in templates that includes non-Page items; passing the output of `.GroupBy` directly instead of ranging over groups.","solutions":["Pass an actual page collection (e.g. `.Site.RegularPages`, `where .Site.RegularPages ...`) to pagination/grouping functions.","When working with grouped results, range over the groups and use each `.Pages` field.","If building a custom slice, ensure every element is a Page — check with `{{ printf \"%T\" $x }}`.","For data-file content, render it directly with `range` instead of page-collection functions."],"exampleFix":"// before\n{{ $p := .Paginate .Site.Data.products }}\n// after\n{{ $p := .Paginate (where .Site.RegularPages \"Section\" \"products\") }}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Check convertibility before calling page.ToPages\nfunc isPagesLike(v any) bool {\n    switch v.(type) {\n    case page.Pages, []page.Page, []any, nil:\n        return true\n    default:\n        return false\n    }\n}","tryCatchPattern":"pages, err := page.ToPages(v)\nif err != nil {\n    return fmt.Errorf(\"expected a Pages collection, got %T: %w\", v, err)\n}","preventionTips":["Don't pass scalar values or maps where a Pages collection is expected in templates","Keep template variables that hold page collections clearly named (e.g. $pages)","Test templates with empty and non-empty collections"],"tags":["hugo","templates","pages","pagination","type-conversion"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}