{"id":"f668d11629ece2ab","repo":"gohugoio/hugo","slug":"must-provide-targetpath-the-template-data-context","errorCode":null,"errorMessage":"must provide targetPath, the template data context and a Resource object","messagePattern":"must provide targetPath, the template data context and a Resource object","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/resources/resources.go","lineNumber":243,"sourceCode":"// FromString creates a Resource from a string published to the relative target path.\nfunc (ns *Namespace) FromString(targetPathIn, contentIn any) (resource.Resource, error) {\n\ttargetPath, err := cast.ToStringE(targetPathIn)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tcontent, err := cast.ToStringE(contentIn)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn ns.createClient.FromString(targetPath, content)\n}\n\n// ExecuteAsTemplate creates a Resource from a Go template, parsed and executed with\n// the given data, and published to the relative target path.\nfunc (ns *Namespace) ExecuteAsTemplate(ctx context.Context, args ...any) (resource.Resource, error) {\n\tif len(args) != 3 {\n\t\treturn nil, fmt.Errorf(\"must provide targetPath, the template data context and a Resource object\")\n\t}\n\ttargetPath, err := cast.ToStringE(args[0])\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdata := args[1]\n\n\tr, ok := args[2].(resources.ResourceTransformer)\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"type %T not supported in Resource transformations\", args[2])\n\t}\n\n\treturn ns.templatesClient.ExecuteAsTemplate(ctx, r, targetPath, data)\n}\n\n// Fingerprint transforms the given Resource with a MD5 hash of the content in\n// the RelPermalink and Permalink.\nfunc (ns *Namespace) Fingerprint(args ...any) (resource.Resource, error) {","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/resources/resources.go#L225-L261","documentation":"`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.)","triggerScenarios":"`{{ 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.","commonSituations":"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).","solutions":["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.","Check the order: targetPath first, data second, resource last.","Pass `.` (or site) as the data context even if the template body doesn't use it."],"exampleFix":"<!-- before -->\n{{ $css := resources.ExecuteAsTemplate \"css/main.css\" $tpl }}\n<!-- after -->\n{{ $css := resources.ExecuteAsTemplate \"css/main.css\" . $tpl }}","handlingStrategy":"validation","validationCode":"{{ $tpl := resources.Get \"template.tmpl\" }}{{ if $tpl }}{{ $out := resources.ExecuteAsTemplate \"out.css\" . $tpl }}{{ end }}","typeGuard":"{{ $ok := and (eq (printf \"%T\" $target) \"string\") $resource }}","tryCatchPattern":"{{ with try (resources.ExecuteAsTemplate $target $ctx $res) }}{{ with .Err }}{{ errorf \"ExecuteAsTemplate failed: %s\" . }}{{ end }}{{ end }}","preventionTips":["Always pass exactly three args: targetPath string, data context, then the Resource","Verify resources.Get didn't return nil before passing it as the third argument","Keep the argument order straight — the Resource comes last"],"tags":["hugo","resources","execute-as-template","arguments"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}