{"record":{"id":"af269126e0ab122b","repo":"gohugoio/hugo","slug":"can-t-apply-over-v","errorCode":null,"errorMessage":"can't apply over %v","messagePattern":"can't apply over (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/apply.go","lineNumber":63,"sourceCode":"\t}\n\n\tswitch seqv.Kind() {\n\tcase reflect.Array, reflect.Slice:\n\t\tr := make([]any, seqv.Len())\n\t\tfor i := range seqv.Len() {\n\t\t\tvv := seqv.Index(i)\n\n\t\t\tvvv, err := applyFnToThis(ctx, fnv, vv, args...)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\n\t\t\tr[i] = vvv.Interface()\n\t\t}\n\n\t\treturn r, nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"can't apply over %v\", c)\n\t}\n}\n\nvar typeOfReflectValue = reflect.TypeFor[reflect.Value]()\n\nfunc applyFnToThis(ctx context.Context, fn, this reflect.Value, args ...any) (reflect.Value, error) {\n\tnum := fn.Type().NumIn()\n\tif num > 0 && hreflect.IsContextType(fn.Type().In(0)) {\n\t\targs = append([]any{ctx}, args...)\n\t}\n\n\tn := make([]reflect.Value, len(args))\n\tfor i, arg := range args {\n\t\tif arg == \".\" {\n\t\t\tn[i] = this\n\t\t} else {\n\t\t\tn[i] = reflect.ValueOf(arg)\n\t\t}","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/apply.go#L45-L81","documentation":"Returned by the collections.Apply template function when the input collection c is not nil but its reflected Kind is neither Array nor Slice. Apply iterates the collection applying fname to each element; non-iterable kinds (map, struct, number, etc.) hit the default branch.","triggerScenarios":"Calling `apply collection \"function\" args...` in a template where collection is a map, scalar, struct, or channel — anything whose reflect.Kind is not Array/Slice. Nil is handled earlier (returns empty slice).","commonSituations":"Passing .Params (a map) instead of .Params.tags (a slice) to apply; passing a Page object instead of .Pages; a variable that resolved to a scalar after a failed lookup.","solutions":["Pass an array or slice to apply — e.g. use `. .Pages` or `.Params.tags` rather than the parent map/object.","Guard with `if reflect.IsSlice` / `len` checks before calling apply, or use `slice` / `delimit` helpers.","For maps, extract a slice of values first (e.g. via a custom partial) before applying."],"exampleFix":"// before\n{{ $titles := apply .Params \"partial getTitle\" }}\n\n// after\n{{ $titles := apply .Params.tags \"partial getTitle\" }}","handlingStrategy":"type-guard","validationCode":"// Guard that the value is a slice/array before calling apply.\nfunc isSliceOrArray(v any) bool {\n    switch reflect.ValueOf(v).Kind() {\n    case reflect.Slice, reflect.Array: return true\n    }\n    return false\n}","typeGuard":"// Go type guard narrowing to slice/array.\nfunc asSlice(v any) ([]any, bool) {\n    rv := reflect.ValueOf(v)\n    if rv.Kind() != reflect.Slice && rv.Kind() != reflect.Array { return nil, false }\n    out := make([]any, rv.Len())\n    for i := range rv.Len() { out[i] = rv.Index(i).Interface() }\n    return out, true\n}","tryCatchPattern":null,"preventionTips":["Always pass an actual list (e.g. .Pages, .Params.tags) to apply.","Guard template calls with `{{ if reflect.IsSlice .X }}`.","Distinguish nil (returns empty slice) from wrong-type inputs."],"tags":["collections","apply","template","type-mismatch","reflection"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}