{"record":{"id":"41fe87fc4e5117ee","repo":"gohugoio/hugo","slug":"need-at-least-2-arguments-to-append","errorCode":null,"errorMessage":"need at least 2 arguments to append","messagePattern":"need at least 2 arguments to append","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/append.go","lineNumber":33,"sourceCode":"\nimport (\n\t\"errors\"\n\n\t\"github.com/gohugoio/hugo/common/collections\"\n)\n\n// Append appends args up to the last one to the slice in the last argument.\n// This construct allows template constructs like this:\n//\n//\t{{ $pages = $pages | append $p2 $p1 }}\n//\n// Note that with 2 arguments where both are slices of the same type,\n// the first slice will be appended to the second:\n//\n//\t{{ $pages = $pages | append .Site.RegularPages }}\nfunc (ns *Namespace) Append(args ...any) (any, error) {\n\tif len(args) < 2 {\n\t\treturn nil, errors.New(\"need at least 2 arguments to append\")\n\t}\n\n\tto := args[len(args)-1]\n\tfrom := args[:len(args)-1]\n\n\treturn collections.Append(to, from...)\n}\n","sourceCodeStart":15,"sourceCodeEnd":41,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/append.go#L15-L41","documentation":"Append (append.go:31) takes variadic args where the LAST arg is the destination slice and the rest are elements to add. With fewer than 2 args there is no element+slice pair, so the call is rejected at line 33 before any reflection. The message is an explicit arity guard.","triggerScenarios":"Calling {{ append .Pages }} with one arg, or {{ $pages | append }} where the pipe supplies the only argument (len(args) < 2). The pipe target becomes the sole positional arg.","commonSituations":"Refactoring a template and dropping the appended value; piping a slice into append but forgetting to pass the element to add; calling append intending to clone a single slice.","solutions":["Pass at least one element plus the destination slice: {{ $pages | append .Page }}","To merge two slices of the same element type, pass both: {{ append $morePages $pages }}","When piping, always provide the element(s) after the pipe target"],"exampleFix":"// before\n{{ $pages | append }}\n// after\n{{ $pages | append .Page }}","handlingStrategy":"validation","validationCode":"{{/* ensure at least one element plus the slice before appending */}}\n{{ if and $elem $pages }}\n  {{ $pages = $pages | append $elem }}\n{{ end }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass the destination slice as the last positional arg and at least one element before it","When piping, remember the pipe target is the final arg, so you still need the element(s)","Treat append as needing (element..., slice) not (slice) alone"],"tags":["hugo","go-template","collections","argument-validation","arity"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}