mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-21 21:35:28 +02:00
Merge commit '8b9803425e63e1b1801f8d5d676e96368d706722'
This commit is contained in:
@@ -13,12 +13,27 @@ action:
|
||||
- functions/safe/URL
|
||||
returnType: template.JSStr
|
||||
signatures: [safe.JSStr INPUT]
|
||||
toc: true
|
||||
aliases: [/functions/safejsstr]
|
||||
---
|
||||
|
||||
Encapsulates a sequence of characters meant to be embedded between quotes in a JavaScript expression. 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.
|
||||
|
||||
Without declaring a variable to be a safe JavaScript string:
|
||||
## Introduction
|
||||
|
||||
{{% include "functions/_common/go-html-template-package.md" %}}
|
||||
|
||||
## Usage
|
||||
|
||||
Use the `safe.JSStr` function to encapsulate a sequence of characters meant to be embedded between quotes in a JavaScript expression.
|
||||
|
||||
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.
|
||||
|
||||
See the [Go documentation] for details.
|
||||
|
||||
[Go documentation]: https://pkg.go.dev/html/template#JSStr
|
||||
|
||||
## Example
|
||||
|
||||
Without a safe declaration:
|
||||
|
||||
```go-html-template
|
||||
{{ $title := "Lilo & Stitch" }}
|
||||
@@ -27,7 +42,7 @@ Without declaring a variable to be a safe JavaScript string:
|
||||
</script>
|
||||
```
|
||||
|
||||
Rendered:
|
||||
Hugo renders the above to:
|
||||
|
||||
```html
|
||||
<script>
|
||||
@@ -35,7 +50,7 @@ Rendered:
|
||||
</script>
|
||||
```
|
||||
|
||||
To avoid escaping by Go's [html/template] package:
|
||||
To declare the string as safe:
|
||||
|
||||
```go-html-template
|
||||
{{ $title := "Lilo & Stitch" }}
|
||||
@@ -44,12 +59,10 @@ To avoid escaping by Go's [html/template] package:
|
||||
</script>
|
||||
```
|
||||
|
||||
Rendered:
|
||||
Hugo renders the above to:
|
||||
|
||||
```html
|
||||
<script>
|
||||
const a = "Title: " + "Lilo & Stitch";
|
||||
</script>
|
||||
```
|
||||
|
||||
[html/template]: https://pkg.go.dev/html/template
|
||||
|
Reference in New Issue
Block a user