You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/en/functions/go-template/return.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ action:
13
13
toc: true
14
14
---
15
15
16
-
The `return` statement is a custom addition to Go's [text/template] package. Used within partial templates, the `return` statement terminates template execution and returns the given value, if any.
16
+
The `return` statement is a non-standard extension to Go's [text/template package]. Used within partial templates, the `return` statement terminates template execution and returns the given value, if any.
17
17
18
18
The returned value may be of any data type including, but not limited to, [`bool`], [`float`], [`int`], [`map`], [`resource`], [`slice`], and [`string`].
description: Returns a TryValue object after evaluating the given expression.
4
+
categories: []
5
+
keywords: []
6
+
action:
7
+
aliases: []
8
+
related: []
9
+
returnType: TryValue
10
+
signatures: ['try EXPRESSION']
11
+
toc: true
12
+
---
13
+
14
+
{{< new-in 0.141.0 >}}
15
+
16
+
The `try` statement is a non-standard extension to Go's [text/template] package. It introduces a mechanism for handling errors within templates, mimicking the `try-catch` constructs found in other programming languages.
17
+
18
+
[text/template]: https://pkg.go.dev/text/template
19
+
20
+
## Methods
21
+
22
+
The `TryValue` object encapsulates the result of evaluating the expression, and provides two methods:
23
+
24
+
Err
25
+
: (`string`) Returns a string representation of the error thrown by the expression, if an error occurred, or returns `nil` if the expression evaluated without errors.
26
+
27
+
Value
28
+
: (`any`) Returns the result of the expression if the evaluation was successful, or returns `nil` if an error occurred while evaluating the expression.
Error handling is essential when using the [`resources.GetRemote`] function to capture remote resources such as data or images. When calling this function, if the HTTP request fails, Hugo will fail the build.
{{ errorf "Unable to get remote resource %q" $url }}
28
30
{{ end }}
29
-
{{ else }}
30
-
{{ errorf "Unable to get remote resource %q" $url }}
31
31
{{ end }}
32
32
```
33
33
@@ -75,14 +75,14 @@ When retrieving remote data, use the [`transform.Unmarshal`] function to [unmars
75
75
```go-html-template
76
76
{{ $data := dict }}
77
77
{{ $url := "https://example.org/books.json" }}
78
-
{{ with resources.GetRemote $url }}
78
+
{{ with try (resources.GetRemote $url) }}
79
79
{{ with .Err }}
80
80
{{ errorf "%s" . }}
81
-
{{ else }}
81
+
{{ else with .Value }}
82
82
{{ $data = . | transform.Unmarshal }}
83
+
{{ else }}
84
+
{{ errorf "Unable to get remote resource %q" $url }}
83
85
{{ end }}
84
-
{{ else }}
85
-
{{ errorf "Unable to get remote resource %q" $url }}
86
86
{{ end }}
87
87
```
88
88
@@ -98,39 +98,39 @@ In these cases, pass the resource `Content` through the `transform.Unmarshal` fu
98
98
99
99
## Error handling
100
100
101
-
The [`Err`]method on a resource returned by the `resources.GetRemote` function returns an error message if the HTTP request fails, else nil. If you do not handle the error yourself, Hugo will fail the build.
101
+
Use the [`try`]statement to capture HTTP request errors. If you do not handle the error yourself, Hugo will fail the build.
102
102
103
-
[`Err`]: /methods/resource/err/
103
+
[`try`]: /functions/go-template/try
104
104
105
105
{{% note %}}
106
106
Hugo does not classify an HTTP response with status code 404 as an error. In this case the function returns nil.
0 commit comments