{"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/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/collections/append.go#L15-L41","documentation":"Hugo's `append` template function (tpl/collections/append.go:33) requires at least two arguments: one or more values to append plus the target slice as the last argument. With fewer than two there is nothing to append onto, so it errors immediately.","triggerScenarios":"Calling `{{ append $x }}` with a single argument, or `{{ $s = append $s }}` — including the pipe form `{{ $s | append }}` where nothing precedes the pipe, so only the piped slice arrives.","commonSituations":"Refactoring a loop and deleting the value being appended, conditional code where the value variable ends up omitted, or misunderstanding that with `|` the piped value is the target slice and at least one element must still be given as a function argument.","solutions":["Provide both the element(s) and the target slice: `{{ $s = $s | append $item }}` or `{{ $s = append $item $s }}`.","If you meant to concatenate two slices, pass both: `{{ $pages = $pages | append .Site.RegularPages }}`.","Initialize the target with `slice` first if it may be undefined: `{{ $s := slice }}`."],"exampleFix":"<!-- before -->\n{{ $s = append $s }}\n<!-- after -->\n{{ $s = $s | append $item }}","handlingStrategy":"validation","validationCode":"{{/* append needs the target slice plus at least one element */}}\n{{ $s := slice }}\n{{ with .items }}{{ $s = $s | append . }}{{ end }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always call append with the slice as the final piped argument plus at least one value","Guard optional values with `with` so you never call append with a single argument","Initialize accumulators with (slice) before loops that append","Remember pipe syntax: `$s | append $v` counts as 2 args; a bare `append $s` does not"],"tags":["hugo","templates","collections","arguments"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}