Debian Codegraph @

docs/content/en/functions/go-template/return.md

raw
1 ---
2 title: return
3 description: Terminates execution of the current template and, when used within a partial template, returns the given value, if any.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: []
9 returnType: any
10 signatures: ['return [VALUE]']
11 ---
12
13 The `return` statement is a non-standard extension to Go's [`text/template`][] package. It terminates execution of the current template only; execution continues in any calling template. When used within a _partial_ template, the `return` statement may also return a value to the caller.
14
15 {{< new-in 0.166.0 >}}
16 In earlier versions, the `return` statement was only supported within partial templates, limited to one `return` statement per template, executed regardless of its position within logical blocks. The `return` statement now follows normal flow control: you can use it in any template, in any position, any number of times, and Hugo executes it only when reached.
17 {{< /new-in >}}
18
19 ## Return a value
20
21 Within a _partial_ template, the `return` statement may return a value of any data type: [`bool`](g), [`float`](g), [`int`](g), [`map`](g), [`resource`](g), [`slice`](g), [`string`](g), and others. When returning a value, any output rendered before the `return` statement is discarded.
22
23 Using the `return` statement with a value in any other template type produces an error.
24
25 For example, a _partial_ template that returns a string value:
26
27 ```go-html-template {file="layouts/_partials/parity.html"}
28 {{ if math.ModBool . 2 }}
29 {{ return "even" }}
30 {{ end }}
31 {{ return "odd" }}
32 ```
33
34 ```go-html-template {file="layouts/single.html"}
35 {{ partial "parity.html" 42 }} → even
36 ```
37
38 A more practical example is a _partial_ template that returns the cover image of the first page in a section that has one:
39
40 ```go-html-template {file="layouts/_partials/section-cover.html"}
41 {{ range .Pages }}
42 {{ with .Resources.GetMatch "cover.*" }}
43 {{ return . }}
44 {{ end }}
45 {{ end }}
46 ```
47
48 ```go-html-template {file="layouts/section.html"}
49 {{ with partial "section-cover.html" . }}
50 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
51 {{ end }}
52 ```
53
54 ## Return early
55
56 Use the `return` statement without a value to stop execution of the current template:
57
58 ```go-html-template {file="layouts/single.html"}
59 <h2>{{ .Title }}</h2>
60 {{ if .Draft }}
61 <p>This article is a draft.</p>
62 {{ return }}
63 {{ end }}
64 {{ .Content }}
65 ```
66
67 Within a _shortcode_ template, use a `return` statement after each validation check to avoid deeply nested conditional blocks:
68
69 ```go-html-template {file="layouts/_shortcodes/img.html"}
70 {{ if not (.Get "src") }}
71 {{ errorf "The %q shortcode requires a src argument. See %s" .Name .Position }}
72 {{ return }}
73 {{ end }}
74
75 {{ if not (.Get "alt") }}
76 {{ errorf "The %q shortcode requires an alt argument. See %s" .Name .Position }}
77 {{ return }}
78 {{ end }}
79
80 <img src="{{ .Get "src" }}" alt="{{ .Get "alt" }}">
81 ```
82
83 ## Limitations
84
85 The `return` statement must be the last command in a pipeline. This produces an error:
86
87 ```go-html-template
88 {{ return "even" | strings.ToUpper }}
89 ```
90
91 [`text/template`]: https://pkg.go.dev/text/template