| 1 |
--- |
| 2 |
title: safe.HTML |
| 3 |
description: Returns the given string declared as safe HTML. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [safeHTML] |
| 9 |
returnType: template.HTML |
| 10 |
signatures: [safe.HTML INPUT] |
| 11 |
aliases: [/functions/safehtml] |
| 12 |
--- |
| 13 |
|
| 14 |
## Introduction |
| 15 |
|
| 16 |
{{% include "/_common/functions/go-html-template-package.md" %}} |
| 17 |
|
| 18 |
## Usage |
| 19 |
|
| 20 |
Use the `safe.HTML` function to encapsulate a known safe HTML document fragment. It should not be used for HTML from a third-party, or HTML with unclosed tags or comments. |
| 21 |
|
| 22 |
Use of this type presents a security risk: the encapsulated content should come from a trusted source, as it will be included verbatim in the template output. |
| 23 |
|
| 24 |
See the [Go documentation][] for details. |
| 25 |
|
| 26 |
## Example |
| 27 |
|
| 28 |
Without a safe declaration: |
| 29 |
|
| 30 |
```go-html-template |
| 31 |
{{ $html := "<em>emphasized</em>" }} |
| 32 |
{{ $html }} |
| 33 |
``` |
| 34 |
|
| 35 |
Hugo renders the above to: |
| 36 |
|
| 37 |
```html |
| 38 |
<em>emphasized</em> |
| 39 |
``` |
| 40 |
|
| 41 |
To declare the string as safe: |
| 42 |
|
| 43 |
```go-html-template |
| 44 |
{{ $html := "<em>emphasized</em>" }} |
| 45 |
{{ $html | safeHTML }} |
| 46 |
``` |
| 47 |
|
| 48 |
Hugo renders the above to: |
| 49 |
|
| 50 |
```html |
| 51 |
<em>emphasized</em> |
| 52 |
``` |
| 53 |
|
| 54 |
[Go documentation]: https://pkg.go.dev/html/template#HTML |