gohugoio/hugo · error

need at least 2 arguments to append

Error message

need at least 2 arguments to append

What it means

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.

Source

Thrown at tpl/collections/append.go:33

import (
	"errors"

	"github.com/gohugoio/hugo/common/collections"
)

// Append appends args up to the last one to the slice in the last argument.
// This construct allows template constructs like this:
//
//	{{ $pages = $pages | append $p2 $p1 }}
//
// Note that with 2 arguments where both are slices of the same type,
// the first slice will be appended to the second:
//
//	{{ $pages = $pages | append .Site.RegularPages }}
func (ns *Namespace) Append(args ...any) (any, error) {
	if len(args) < 2 {
		return nil, errors.New("need at least 2 arguments to append")
	}

	to := args[len(args)-1]
	from := args[:len(args)-1]

	return collections.Append(to, from...)
}

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Provide both the element(s) and the target slice: `{{ $s = $s | append $item }}` or `{{ $s = append $item $s }}`.
  2. If you meant to concatenate two slices, pass both: `{{ $pages = $pages | append .Site.RegularPages }}`.
  3. Initialize the target with `slice` first if it may be undefined: `{{ $s := slice }}`.

Example fix

<!-- before -->
{{ $s = append $s }}
<!-- after -->
{{ $s = $s | append $item }}
Defensive patterns

Strategy: validation

Validate before calling

{{/* append needs the target slice plus at least one element */}}
{{ $s := slice }}
{{ with .items }}{{ $s = $s | append . }}{{ end }}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/41fe87fc4e5117ee.json. Report an issue: GitHub ↗.