gohugoio/hugo · error

Defer does not take any arguments

Error message

Defer does not take any arguments

What it means

Thrown by templates.Defer when len(args) != 0. The Defer function takes no positional arguments; configuration is done via the block's own (…) syntax with DeferOpts fields, not function arguments.

Solutions

  1. Use the block syntax for options: {{ templates.Defer (key="mykey" data=.) }} … {{ end }}.
  2. Remove any positional arguments from the templates.Defer call.
  3. Confirm you are using the block form {{ templates.Defer }}…{{ end }} and not a function call form.

Example fix

// before (positional args):
{{ templates.Defer "mykey" }}
// after (no function args; use block opts):
{{ templates.Defer (key="mykey") }}
  ...content...
{{ end }}
Defensive patterns

Strategy: validation

Validate before calling

{{ templates.Defer }}…{{ end }}
// or with block opts only:
{{ templates.Defer (key="k") }}…{{ end }}

Prevention

When it happens

Trigger: Calling {{ templates.Defer "key" }} or passing any positional argument inside the function parentheses: templates.Defer $key $data. The function signature is Defer(ctx, args ...any) and rejects any args.

Common situations: Confusing templates.Defer's function args with the block's DeferOpts. Users coming from resources.PostProcess or other functions try to pass a key or data as a positional arg.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/4fa2dcbd99298cc4. Report an issue: GitHub.

Appendix: source

Thrown at tpl/templates/templates.go:69

func (ns *Namespace) Exists(name string) bool {
	return ns.deps.GetTemplateStore().HasTemplate(name)
}

// Defer defers the execution of a template block.
func (ns *Namespace) Defer(ctx context.Context, args ...any) (bool, error) {
	// Prevent defer from being used in content adapters,
	// that just doesn't work.
	ns.deps.Site.CheckReady()

	// Reject use inside a partialCached body: the cached output retains the
	// deferred placeholder across rebuilds while the executions map is reset,
	// which used to cause a panic in executeDeferredTemplates. See issue #13492.
	if tpl.Context.IsInPartialCached.Get(ctx) {
		return false, fmt.Errorf("templates.Defer cannot be used inside a partialCached partial; use partial instead, or move templates.Defer to the calling template")
	}

	if len(args) != 0 {
		return false, fmt.Errorf("Defer does not take any arguments")
	}
	return true, nil
}

var defferedIDCounter atomic.Uint64

type DeferOpts struct {
	// Optional cache key. If set, the deferred block will be executed
	// once per unique key.
	Key string

	// Optional data context to use when executing the deferred block.
	Data any
}

// Inner executes the inner content of a partial decorator.
// Note that there is only one inner block per partial decorator, but inner may be called multiple times with, typically, different data.
func (ns *Namespace) Inner(ctx context.Context, data any) (any, error) {

View on GitHub (pinned to 52c9bd7908)