gohugoio/hugo · error

must provide targetPath, the template data context and a Res

Error message

must provide targetPath, the template data context and a Resource object

What it means

`resources.ExecuteAsTemplate` takes exactly three arguments: the target path, the data context to execute the template with, and the Resource whose content is the template. Any other argument count fails immediately. (A wrong type in the third position produces a separate "type %T not supported" error.)

Source

Thrown at tpl/resources/resources.go:243

// FromString creates a Resource from a string published to the relative target path.
func (ns *Namespace) FromString(targetPathIn, contentIn any) (resource.Resource, error) {
	targetPath, err := cast.ToStringE(targetPathIn)
	if err != nil {
		return nil, err
	}
	content, err := cast.ToStringE(contentIn)
	if err != nil {
		return nil, err
	}

	return ns.createClient.FromString(targetPath, content)
}

// ExecuteAsTemplate creates a Resource from a Go template, parsed and executed with
// the given data, and published to the relative target path.
func (ns *Namespace) ExecuteAsTemplate(ctx context.Context, args ...any) (resource.Resource, error) {
	if len(args) != 3 {
		return nil, fmt.Errorf("must provide targetPath, the template data context and a Resource object")
	}
	targetPath, err := cast.ToStringE(args[0])
	if err != nil {
		return nil, err
	}
	data := args[1]

	r, ok := args[2].(resources.ResourceTransformer)
	if !ok {
		return nil, fmt.Errorf("type %T not supported in Resource transformations", args[2])
	}

	return ns.templatesClient.ExecuteAsTemplate(ctx, r, targetPath, data)
}

// Fingerprint transforms the given Resource with a MD5 hash of the content in
// the RelPermalink and Permalink.
func (ns *Namespace) Fingerprint(args ...any) (resource.Resource, error) {

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Call with all three: `{{ $css := resources.Get "css/main.tpl.css" | resources.ExecuteAsTemplate "css/main.css" . }}` — the pipe supplies the resource as the third argument.
  2. Check the order: targetPath first, data second, resource last.
  3. Pass `.` (or site) as the data context even if the template body doesn't use it.

Example fix

<!-- before -->
{{ $css := resources.ExecuteAsTemplate "css/main.css" $tpl }}
<!-- after -->
{{ $css := resources.ExecuteAsTemplate "css/main.css" . $tpl }}
Defensive patterns

Strategy: validation

Validate before calling

{{ $tpl := resources.Get "template.tmpl" }}{{ if $tpl }}{{ $out := resources.ExecuteAsTemplate "out.css" . $tpl }}{{ end }}

Type guard

{{ $ok := and (eq (printf "%T" $target) "string") $resource }}

Try / catch

{{ with try (resources.ExecuteAsTemplate $target $ctx $res) }}{{ with .Err }}{{ errorf "ExecuteAsTemplate failed: %s" . }}{{ end }}{{ end }}

Prevention

When it happens

Trigger: `{{ resources.ExecuteAsTemplate "style.css" $resource }}` — omitting the data context; or passing extra arguments. The correct order is targetPath, data, resource — swapping data and resource also commonly leads here or to the type error.

Common situations: Forgetting to pass `.` as the data context; following outdated snippets with a different argument order; piping the resource in a way that drops an argument (`$r | resources.ExecuteAsTemplate "out.css"` only supplies two).

Related errors


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