{"id":"fb8d7bda69229d3f","repo":"gohugoio/hugo","slug":"wrong-number-of-args-for-default-want-2-got-d","errorCode":null,"errorMessage":"wrong number of args for default: want 2 got %d","messagePattern":"wrong number of args for default: want 2 got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/compare/compare.go","lineNumber":57,"sourceCode":"\tloc *time.Location\n\t// Enable to do case insensitive string compares.\n\tcaseInsensitive bool\n}\n\n// Default checks whether a givenv is set and returns the default value defaultv if it\n// is not.  \"Set\" in this context means non-zero for numeric types and times;\n// non-zero length for strings, arrays, slices, and maps;\n// any boolean or struct value; or non-nil for any other types.\nfunc (*Namespace) Default(defaultv any, givenv ...any) (any, error) {\n\t// given is variadic because the following construct will not pass a piped\n\t// argument when the key is missing:  {{ index . \"key\" | default \"foo\" }}\n\t// The Go template will complain that we got 1 argument when we expected 2.\n\n\tif len(givenv) == 0 {\n\t\treturn defaultv, nil\n\t}\n\tif len(givenv) != 1 {\n\t\treturn nil, fmt.Errorf(\"wrong number of args for default: want 2 got %d\", len(givenv)+1)\n\t}\n\n\tg := reflect.ValueOf(givenv[0])\n\tif !g.IsValid() {\n\t\treturn defaultv, nil\n\t}\n\n\tset := false\n\n\tswitch g.Kind() {\n\tcase reflect.Bool:\n\t\tset = true\n\tcase reflect.String, reflect.Array, reflect.Slice, reflect.Map:\n\t\tset = g.Len() != 0\n\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\n\t\tset = g.Int() != 0\n\tcase reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:\n\t\tset = g.Uint() != 0","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/compare/compare.go#L39-L75","documentation":"The `default` template function takes exactly one default value and one given value. Its second parameter is variadic only to tolerate the piped-with-missing-key case; if more than one 'given' value arrives, Hugo reports the total argument count (len+1) with this error.","triggerScenarios":"`{{ default \"x\" .A .B }}` — calling default with three or more arguments, or piping into a default that already has two explicit arguments (`{{ .A | default \"x\" .B }}`).","commonSituations":"Misunderstanding argument order and adding extras, confusing `default` with a coalesce-style function that scans multiple candidates, or a pipe silently appending the piped value as an extra argument after two explicit ones.","solutions":["Use exactly two values: `{{ default \"fallback\" .Value }}` or `{{ .Value | default \"fallback\" }}`.","For multiple fallbacks, chain: `{{ .A | default .B | default \"x\" }}`.","When piping, remove the explicit second argument — the piped value fills it."],"exampleFix":"{{/* before */}}\n{{ .Params.title | default .Site.Title \"Untitled\" }}\n{{/* after */}}\n{{ .Params.title | default .Site.Title | default \"Untitled\" }}","handlingStrategy":"validation","validationCode":"{{/* default takes exactly two args: default DEFAULT INPUT */}}\n{{ $v := default \"fallback\" .Params.subtitle }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always call default with exactly two arguments: the default value first, then the input","In pipelines, remember the piped value becomes the last argument: {{ .Params.x | default \"y\" }} is already two args — don't add another","Parenthesize nested calls so argument grouping is explicit: default \"x\" (index .Params \"key\")"],"tags":["hugo","templates","default","arguments"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}