{"id":"319fd02c7b0bf259","repo":"gohugoio/hugo","slug":"argument-must-be-a-slice","errorCode":null,"errorMessage":"argument must be a slice","messagePattern":"argument must be a slice","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/collections.go","lineNumber":401,"sourceCode":"\n\tif limitv > seqv.Len() {\n\t\tlimitv = seqv.Len()\n\t}\n\n\treturn seqv.Slice(seqv.Len()-limitv, seqv.Len()).Interface(), nil\n}\n\n// Reverse creates a copy of the list l and reverses it.\nfunc (ns *Namespace) Reverse(l any) (any, error) {\n\tif l == nil {\n\t\treturn nil, nil\n\t}\n\tv := reflect.ValueOf(l)\n\n\tswitch v.Kind() {\n\tcase reflect.Slice:\n\tdefault:\n\t\treturn nil, errors.New(\"argument must be a slice\")\n\t}\n\n\tsliceCopy := reflect.MakeSlice(v.Type(), v.Len(), v.Len())\n\n\tfor i := v.Len() - 1; i >= 0; i-- {\n\t\telement := sliceCopy.Index(i)\n\t\telement.Set(v.Index(v.Len() - 1 - i))\n\t}\n\n\treturn sliceCopy.Interface(), nil\n}\n\n// Sanity check for slices created by Seq and D.\nconst maxSeqSize = 1000000\n\nvar errSeqSizeExceedsLimit = errors.New(\"size of result exceeds limit\")\n\n// Seq creates a sequence of integers from args. It's named and used as GNU's seq.","sourceCodeStart":383,"sourceCodeEnd":419,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/collections/collections.go#L383-L419","documentation":"The `collections.Reverse` template function only accepts a slice; it reflects on the argument's kind and rejects anything that is not `reflect.Slice`. Arrays, maps, strings, and page collections that aren't slices trigger this error.","triggerScenarios":"Calling `{{ collections.Reverse $x }}` where `$x` is a map, string, integer, or other non-slice value. A nil argument returns nil without error; only non-nil non-slices fail.","commonSituations":"Trying to reverse a string character-by-character; passing a dict/map from front matter; passing a single page instead of a page collection.","solutions":["Ensure the argument is a slice: `{{ collections.Reverse (slice 1 2 3) }}` or a page collection like `.Pages`.","To reverse sorted pages, prefer `.Pages.ByDate.Reverse` on page collections.","Convert other data to a slice first (e.g. via `slice` or `sort`) before reversing."],"exampleFix":"<!-- before -->\n{{ collections.Reverse .Params.settings }}  <!-- a map -->\n<!-- after -->\n{{ collections.Reverse .Params.items }}  <!-- a list in front matter -->","handlingStrategy":"type-guard","validationCode":"{{ if reflect.IsSlice $v }}\n  {{ $shuffled := shuffle $v }}\n{{ else }}\n  {{ warnf \"expected slice, got %T\" $v }}\n{{ end }}","typeGuard":"{{ $isSlice := reflect.IsSlice $v }}","tryCatchPattern":null,"preventionTips":["Guard with `reflect.IsSlice` before calling shuffle/sort-like functions.","Remember `.Params.foo` from front matter may be a scalar or map even if you expect a list; normalize with `slice` when unsure.","Use `default (slice)` to fall back to an empty slice for missing params only when explicitly desired."],"tags":["hugo","templates","reverse","type-mismatch"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}