safeHTMLAttr

将以下英文翻译为中文:

safeHTMLAttr

https://gohugo.io/functions/safehtmlattr/

Declares the provided string as a safe HTML attribute.

语法

safeHTMLAttr INPUT

Given a site configuration that contains this menu entry:

config.

=== “yaml”

``` yaml
menu:
  main:
  - name: IRC
    url: irc://irc.freenode.net/#golang
```

=== “toml”

``` toml
[menu]
[[menu.main]]
  name = 'IRC'
  url = 'irc://irc.freenode.net/#golang'
```

=== “json”

``` json
{
   "menu": {
      "main": [
         {
            "name": "IRC",
            "url": "irc://irc.freenode.net/#golang"
         }
      ]
   }
}
```

Attempting to use the url value directly in an attribute:

1
2
3
{{ range site.Menus.main }}
  <a href="{{ .URL }}">{{ .Name }}</a>
{{ end }}

Will produce:

1
<a href="#ZgotmplZ">IRC</a>

ZgotmplZ is a special value, inserted by Go’s template/html package, that indicates that unsafe content reached a CSS or URL context.

To override the safety check, use the safeHTMLAttr function:

1
2
3
{{ range site.Menus.main }}
  <a {{ printf "href=%q" .URL | safeHTMLAttr }}>{{ .Name }}</a>
{{ end }}

另请参阅