{"record":{"id":"72362fe573e8d63e","repo":"gohugoio/hugo","slug":"failed-to-convert-content-to-string-w","errorCode":null,"errorMessage":"failed to convert content to string: %w","messagePattern":"failed to convert content to string: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/strings/strings.go","lineNumber":57,"sourceCode":"\treturn &Namespace{\n\t\tdeps:          d,\n\t\treplacerCache: hmaps.NewCacheWithOptions[string, *strings.Replacer](hmaps.CacheOptions{Size: 100}),\n\t}\n}\n\n// Namespace provides template functions for the \"strings\" namespace.\n// Most functions mimic the Go stdlib, but the order of the parameters may be\n// different to ease their use in the Go template system.\ntype Namespace struct {\n\tdeps          *deps.Deps\n\treplacerCache *hmaps.Cache[string, *strings.Replacer]\n}\n\n// CountRunes returns the number of runes in s, excluding whitespace.\nfunc (ns *Namespace) CountRunes(s any) (int, error) {\n\tss, err := cast.ToStringE(s)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"failed to convert content to string: %w\", err)\n\t}\n\n\tcounter := 0\n\tfor _, r := range tpl.StripHTML(ss) {\n\t\tif !helpers.IsWhitespace(r) {\n\t\t\tcounter++\n\t\t}\n\t}\n\n\treturn counter, nil\n}\n\n// RuneCount returns the number of runes in s.\nfunc (ns *Namespace) RuneCount(s any) (int, error) {\n\tss, err := cast.ToStringE(s)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"failed to convert content to string: %w\", err)\n\t}","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/strings/strings.go#L39-L75","documentation":"Thrown by strings.CountRunes when cast.ToStringE fails to coerce the input to a string. cast.ToStringE accepts strings, numeric types, and some others; structs, maps, slices, and nil produce an error.","triggerScenarios":"Calling {{ strings.CountRunes . }} where . is a struct/map/slice/nil (e.g. a page object, a params map) rather than a string-like value.","commonSituations":"Passing a page object instead of a field (e.g. .Title), passing a slice of strings, or passing nil from a missing front matter field.","solutions":["Pass a string field: strings.CountRunes .Title.","Coerce explicitly or pick the correct field that yields a string.","Guard against nil front-matter values with a default."],"exampleFix":"// before (whole page object):\n{{ strings.CountRunes . }}\n// after:\n{{ strings.CountRunes .Title }}","handlingStrategy":"type-guard","validationCode":"{{ with .Title }}{{ strings.CountRunes . }}{{ else }}0{{ end }}","typeGuard":"// Pass only string-like front matter fields.\n{{ if reflect.IsMap . }}0{{ else }}{{ strings.CountRunes . }}{{ end }}","tryCatchPattern":null,"preventionTips":["Pass a string field (e.g. .Title), not the whole page object.","Default nil fields to a string with 'with' or 'default'.","Validate front matter fields are strings in your content model."],"tags":["strings","countrunes","type-coercion","template"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}