{"record":{"id":"d98e06eef1d96908","repo":"gohugoio/hugo","slug":"length-argument-must-be-an-integer","errorCode":null,"errorMessage":"length argument must be an integer","messagePattern":"length argument must be an integer","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/strings/strings.go","lineNumber":407,"sourceCode":"\tasRunes := []rune(s)\n\trlen := len(asRunes)\n\n\tvar start, length int\n\n\tswitch len(nums) {\n\tcase 0:\n\t\treturn \"\", errors.New(\"too few arguments\")\n\tcase 1:\n\t\tif start, err = cast.ToIntE(nums[0]); err != nil {\n\t\t\treturn \"\", errors.New(\"start argument must be an integer\")\n\t\t}\n\t\tlength = rlen\n\tcase 2:\n\t\tif start, err = cast.ToIntE(nums[0]); err != nil {\n\t\t\treturn \"\", errors.New(\"start argument must be an integer\")\n\t\t}\n\t\tif length, err = cast.ToIntE(nums[1]); err != nil {\n\t\t\treturn \"\", errors.New(\"length argument must be an integer\")\n\t\t}\n\tdefault:\n\t\treturn \"\", errors.New(\"too many arguments\")\n\t}\n\n\tif rlen == 0 {\n\t\treturn \"\", nil\n\t}\n\n\tif start < 0 {\n\t\tstart += rlen\n\t}\n\n\t// start was originally negative beyond rlen\n\tif start < 0 {\n\t\tstart = 0\n\t}\n","sourceCodeStart":389,"sourceCodeEnd":425,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/strings/strings.go#L389-L425","documentation":"Thrown by strings.Substr when called with two variadic arguments and the second (length) cannot be cast to an integer (tpl/strings/strings.go:406-407). The length controls how many characters to extract and must be a whole number; negative lengths are permitted (omit from the end).","triggerScenarios":"Calling {{ substr .Title 2 \"five\" }} or {{ substr .Title 2 .StringLen }} where the second num is non-integer.","commonSituations":"Passing a human-readable length string ('5 chars'); using a config value typed as string; accidentally passing the ellipsis or a suffix as the length.","solutions":["Coerce the length to int: {{ substr .Title 2 (int .Len) }}.","Confirm the second variadic is the numeric length, not another string.","Store length in front matter as a number."],"exampleFix":"// before\n{{ substr .Title 2 .Len }}\n// after\n{{ substr .Title 2 (int .Len) }}","handlingStrategy":"validation","validationCode":"{{ substr .Title 2 (int .Len) }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["The length is the second variadic; coerce it with (int ...).","Store lengths as numbers in front matter."],"tags":["template-function","strings","substr","type-coercion","argument"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}