gohugoio/hugo · error
truncate requires a length and a string
Error message
truncate requires a length and a string
What it means
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.
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 }}.
Example fix
// before
{{ truncate 50 }}
// after
{{ truncate 50 .Summary }} Defensive patterns
Strategy: validation
Validate before calling
{{ truncate 50 .Title }} Prevention
- Truncate's first arg is the length; always pass the text as a following argument.
- Pipe the text: {{ .Title | truncate 50 }}.
When it happens
Trigger: Calling {{ truncate 50 }} or {{ 50 | truncate }} with no string argument.
Common situations: Piping a number into truncate expecting it to act on the page content; forgetting the second argument; misreading the docs which list length first.
Related errors
- too many arguments passed to truncate
- ellipsis must be a string
- text must be a string
- invalid arguments supplied to `time`
- length argument must be an integer
AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09).
Data as JSON: /api/errors/8fd4b9f09596b5dd.
Report an issue: GitHub.
Appendix: source
Thrown at tpl/strings/truncate.go:54
type htmlTag struct {
name string
pos int
openTag bool
}
// Truncate truncates the string in s to the specified length.
func (ns *Namespace) Truncate(s any, options ...any) (template.HTML, error) {
length, err := cast.ToIntE(s)
if err != nil {
return "", err
}
var textParam any
var ellipsis string
switch len(options) {
case 0:
return "", errors.New("truncate requires a length and a string")
case 1:
textParam = options[0]
ellipsis = " …"
case 2:
textParam = options[1]
ellipsis, err = cast.ToStringE(options[0])
if err != nil {
return "", errors.New("ellipsis must be a string")
}
if _, ok := options[0].(template.HTML); !ok {
ellipsis = html.EscapeString(ellipsis)
}
default:
return "", errors.New("too many arguments passed to truncate")
}
text, err := cast.ToStringE(textParam)
if err != nil {View on GitHub (pinned to 52c9bd7908)