{"record":{"id":"8fd4b9f09596b5dd","repo":"gohugoio/hugo","slug":"truncate-requires-a-length-and-a-string","errorCode":null,"errorMessage":"truncate requires a length and a string","messagePattern":"truncate requires a length and a string","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/strings/truncate.go","lineNumber":54,"sourceCode":"\ntype htmlTag struct {\n\tname    string\n\tpos     int\n\topenTag bool\n}\n\n// Truncate truncates the string in s to the specified length.\nfunc (ns *Namespace) Truncate(s any, options ...any) (template.HTML, error) {\n\tlength, err := cast.ToIntE(s)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tvar textParam any\n\tvar ellipsis string\n\n\tswitch len(options) {\n\tcase 0:\n\t\treturn \"\", errors.New(\"truncate requires a length and a string\")\n\tcase 1:\n\t\ttextParam = options[0]\n\t\tellipsis = \" …\"\n\tcase 2:\n\t\ttextParam = options[1]\n\t\tellipsis, err = cast.ToStringE(options[0])\n\t\tif err != nil {\n\t\t\treturn \"\", errors.New(\"ellipsis must be a string\")\n\t\t}\n\t\tif _, ok := options[0].(template.HTML); !ok {\n\t\t\tellipsis = html.EscapeString(ellipsis)\n\t\t}\n\tdefault:\n\t\treturn \"\", errors.New(\"too many arguments passed to truncate\")\n\t}\n\n\ttext, err := cast.ToStringE(textParam)\n\tif err != nil {","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/strings/truncate.go#L36-L72","documentation":"Thrown by strings.Truncate when no options (the text to truncate) are supplied (tpl/strings/truncate.go:52-54). Truncate's signature is Truncate(s any, options ...any) where s is the length and the first option is the text; calling it with only the length leaves nothing to truncate.","triggerScenarios":"Calling {{ truncate 50 }} or {{ 50 | truncate }} with no string argument.","commonSituations":"Piping a number into truncate expecting it to act on the page content; forgetting the second argument; misreading the docs which list length first.","solutions":["Provide the text as the second argument: {{ truncate 50 .Title }}.","Pipe the text instead: {{ .Title | truncate 50 }}.","Optionally add a custom ellipsis as the middle argument: {{ truncate 50 \"...\" .Title }}."],"exampleFix":"// before\n{{ truncate 50 }}\n// after\n{{ truncate 50 .Summary }}","handlingStrategy":"validation","validationCode":"{{ truncate 50 .Title }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Truncate's first arg is the length; always pass the text as a following argument.","Pipe the text: {{ .Title | truncate 50 }}."],"tags":["template-function","strings","truncate","arity","argument"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}