Debian Codegraph @

docs/content/en/functions/strings/Truncate.md

raw
1 ---
2 title: strings.Truncate
3 description: Returns the given string, truncating it to a maximum length without cutting words or leaving unclosed HTML tags.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: [truncate]
9 returnType: template.HTML
10 signatures: ['strings.Truncate SIZE [ELLIPSIS] STRING']
11 aliases: [/functions/truncate]
12 ---
13
14 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:
15
16 ```go-html-template
17 {{ "<em>Keep my HTML</em>" | safeHTML | strings.Truncate 10 }} → <em>Keep my …</em>
18 ```
19
20 > [!NOTE]
21 > 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.
22
23 [`safe.HTML`]: /functions/safe/html/