HUGO
Menu
GitHub 89803 stars Mastodon

strings.Truncate

Returns the given string, truncating it to a maximum length without cutting words or leaving unclosed HTML tags.
Syntax
strings.Truncate SIZE [ELLIPSIS] STRING
Returns
template.HTML
Alias
truncate

When truncating a value marked as safe HTML, such as one returned by the safe.HTML function, strings.Truncate closes any tag left open by the truncation instead of cutting in the middle of it:

{{ "<em>Keep my HTML</em>" | safeHTML | strings.Truncate 10 }}<em>Keep my …</em>

If you have a raw string containing HTML tags that you want treated as HTML, convert it with the safe.HTML function first. Otherwise, strings.Truncate escapes the tags.