summaryrefslogtreecommitdiffstats
path: root/docs/content/en/functions/fmt/Printf.md
blob: 68df9860944d768c0810a2474c7c511f915f5e8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
---
title: fmt.Printf
description: Formats a string using the standard `fmt.Sprintf` function.
categories: []
keywords: []
params:
  functions_and_methods:
    aliases: [printf]
    returnType: string
    signatures: ['fmt.Printf FORMAT [INPUT]']
aliases: [/functions/printf]
---

{{% include "/_common/functions/fmt/format-string.md" %}}

```go-html-template
{{ $var := "world" }}
{{ printf "Hello %s." $var }} → Hello world.
```

```go-html-template
{{ $pi := 3.14159265 }}
{{ printf "Pi is approximately %.2f." $pi }} → 3.14
```

Use the `printf` function with the `safeHTMLAttr` function:

```go-html-template
{{ $desc := "Eat at Joe's" }}
<meta name="description" {{ printf "content=%q" $desc | safeHTMLAttr }}>
```

Hugo renders this to:

```html
<meta name="description" content="Eat at Joe's">
```