truncate.md (957B)
1 ---
2 title: truncate
3 # linktitle: truncate
4 description: Truncates a text to a max length without cutting words or leaving unclosed HTML tags.
5 date: 2017-02-01
6 publishdate: 2017-02-01
7 lastmod: 2017-02-01
8 categories: [functions]
9 menu:
10 docs:
11 parent: "functions"
12 keywords: [strings]
13 signature: ["truncate SIZE INPUT", "truncate SIZE ELLIPSIS INPUT"]
14 workson: []
15 hugoversion: 19
16 relatedfuncs: []
17 deprecated: false
18 ---
19
20 Since Go templates are HTML-aware, `truncate` will intelligently handle normal strings vs HTML strings:
21
22 ```
23 {{ "<em>Keep my HTML</em>" | safeHTML | truncate 10 }}` → <em>Keep my …</em>`
24 ```
25
26 {{% note %}}
27 If you have a raw string that contains HTML tags you want to remain treated as HTML, you will need to convert the string to HTML using the [`safeHTML` template function](/functions/safehtml) before sending the value to truncate. Otherwise, the HTML tags will be escaped when passed through the `truncate` function.
28 {{% /note %}}