Skip to content

Commit 07d631b

Browse files
committed
Update docs for v0.141.0
Closes gohugoio#2808
1 parent 4429eee commit 07d631b

File tree

19 files changed

+675
-76
lines changed

19 files changed

+675
-76
lines changed

content/en/content-management/content-adapters.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,14 @@ Step 3
193193
{{/* Get remote data. */}}
194194
{{ $data := dict }}
195195
{{ $url := "https://gohugo.io/shared/examples/data/books.json" }}
196-
{{ with resources.GetRemote $url }}
196+
{{ with try (resources.GetRemote $url) }}
197197
{{ with .Err }}
198198
{{ errorf "Unable to get remote resource %s: %s" $url . }}
199-
{{ else }}
199+
{{ else with .Value }}
200200
{{ $data = . | transform.Unmarshal }}
201+
{{ else }}
202+
{{ errorf "Unable to get remote resource %s" $url }}
201203
{{ end }}
202-
{{ else }}
203-
{{ errorf "Unable to get remote resource %s" $url }}
204204
{{ end }}
205205

206206
{{/* Add pages and page resources. */}}
@@ -223,10 +223,10 @@ Step 3
223223
{{/* Add page resource. */}}
224224
{{ $item := . }}
225225
{{ with $url := $item.cover }}
226-
{{ with resources.GetRemote $url }}
226+
{{ with try (resources.GetRemote $url) }}
227227
{{ with .Err }}
228228
{{ errorf "Unable to get remote resource %s: %s" $url . }}
229-
{{ else }}
229+
{{ else with .Value }}
230230
{{ $content := dict "mediaType" .MediaType.Type "value" .Content }}
231231
{{ $params := dict "alt" $item.title }}
232232
{{ $resource := dict
@@ -235,9 +235,9 @@ Step 3
235235
"path" (printf "%s/cover.%s" $item.title .MediaType.SubType)
236236
}}
237237
{{ $.AddResource $resource }}
238+
{{ else }}
239+
{{ errorf "Unable to get remote resource %s" $url }}
238240
{{ end }}
239-
{{ else }}
240-
{{ errorf "Unable to get remote resource %s" $url }}
241241
{{ end }}
242242
{{ end }}
243243

content/en/content-management/image-processing/index.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ Example 3: A more concise way to skip image rendering if the resource is not fou
8888
Example 4: Skips rendering if there's problem accessing a remote resource.
8989

9090
```go-html-template
91-
{{ $u := "https://gohugo.io/img/hugo-logo.png" }}
92-
{{ with resources.GetRemote $u }}
91+
{{ $url := "https://gohugo.io/img/hugo-logo.png" }}
92+
{{ with try (resources.GetRemote $url) }}
9393
{{ with .Err }}
9494
{{ errorf "%s" . }}
95-
{{ else }}
95+
{{ else with .Value }}
9696
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}">
97+
{{ else }}
98+
{{ errorf "Unable to get remote resource %q" $url }}
9799
{{ end }}
98-
{{ else }}
99-
{{ errorf "Unable to get remote resource %q" $u }}
100100
{{ end }}
101101
```
102102

content/en/functions/data/GetCSV.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,16 @@ Consider using the [`resources.GetRemote`] function with [`transform.Unmarshal`]
134134

135135
```go-html-template
136136
{{ $data := dict }}
137-
{{ $u := "https://example.org/pets.csv" }}
138-
{{ with resources.GetRemote $u }}
137+
{{ $url := "https://example.org/pets.csv" }}
138+
{{ with try (resources.GetRemote $url) }}
139139
{{ with .Err }}
140140
{{ errorf "%s" . }}
141-
{{ else }}
141+
{{ else with .Value }}
142142
{{ $opts := dict "delimiter" "," }}
143143
{{ $data = . | transform.Unmarshal $opts }}
144+
{{ else }}
145+
{{ errorf "Unable to get remote resource %q" $url }}
144146
{{ end }}
145-
{{ else }}
146-
{{ errorf "Unable to get remote resource %q" $u }}
147147
{{ end }}
148148
```
149149

content/en/functions/data/GetJSON.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ Consider using the [`resources.GetRemote`] function with [`transform.Unmarshal`]
137137

138138
```go-html-template
139139
{{ $data := dict }}
140-
{{ $u := "https://example.org/books.json" }}
141-
{{ with resources.GetRemote $u }}
140+
{{ $url := "https://example.org/books.json" }}
141+
{{ with try (resources.GetRemote $url) }}
142142
{{ with .Err }}
143143
{{ errorf "%s" . }}
144-
{{ else }}
144+
{{ else with .Value }}
145145
{{ $data = . | transform.Unmarshal }}
146+
{{ else }}
147+
{{ errorf "Unable to get remote resource %q" $url }}
146148
{{ end }}
147-
{{ else }}
148-
{{ errorf "Unable to get remote resource %q" $u }}
149149
{{ end }}
150150
```
151151

content/en/functions/encoding/Base64Decode.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@ https://api.github.com/repos/gohugoio/hugo/readme
2525
To retrieve and render the content:
2626

2727
```go-html-template
28-
{{ $u := "https://api.github.com/repos/gohugoio/hugo/readme" }}
29-
{{ with resources.GetRemote $u }}
28+
{{ $url := "https://api.github.com/repos/gohugoio/hugo/readme" }}
29+
{{ with try (resources.GetRemote $url) }}
3030
{{ with .Err }}
3131
{{ errorf "%s" . }}
32-
{{ else }}
32+
{{ else with .Value}}
3333
{{ with . | transform.Unmarshal }}
3434
{{ .content | base64Decode | markdownify }}
3535
{{ end }}
36+
{{ else }}
37+
{{ errorf "Unable to get remote resource %q" $url }}
3638
{{ end }}
37-
{{ else }}
38-
{{ errorf "Unable to get remote resource %q" $u }}
3939
{{ end }}
4040
```

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ action:
1313
toc: true
1414
---
1515

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.
1717

1818
The returned value may be of any data type including, but not limited to, [`bool`], [`float`], [`int`], [`map`], [`resource`], [`slice`], and [`string`].
1919

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: try
3+
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.
29+
30+
## Explanation
31+
32+
By way of example, let's divide a number by zero:
33+
34+
```go-html-template
35+
{{ $x := 1 }}
36+
{{ $y := 0 }}
37+
{{ $result := div $x $y }}
38+
{{ printf "%v divided by %v equals %v" $x $y .Value }}
39+
```
40+
41+
As expected, the example above throws an error and fails the build:
42+
43+
```terminfo
44+
Error: error calling div: can't divide the value by 0
45+
```
46+
47+
Instead of failing the build, we can catch the error and emit a warning:
48+
49+
```go-html-template
50+
{{ $x := 1 }}
51+
{{ $y := 0 }}
52+
{{ with try (div $x $y) }}
53+
{{ with .Err }}
54+
{{ warnf "%s" . }}
55+
{{ else }}
56+
{{ printf "%v divided by %v equals %v" $x $y .Value }}
57+
{{ end }}
58+
{{ end }}
59+
```
60+
61+
The error thrown by the expression is logged to the console as a warning:
62+
63+
```terminfo
64+
WARN error calling div: can't divide the value by 0
65+
```
66+
67+
Now let's change the arguments to avoid dividing by zero:
68+
69+
```go-html-template
70+
{{ $x := 42 }}
71+
{{ $y := 6 }}
72+
{{ with try (div $x $y) }}
73+
{{ with .Err }}
74+
{{ warnf "%s" . }}
75+
{{ else }}
76+
{{ printf "%v divided by %v equals %v" $x $y .Value }}
77+
{{ end }}
78+
{{ end }}
79+
```
80+
81+
Hugo renders the above to:
82+
83+
```html
84+
42 divided by 6 equals 7
85+
```
86+
87+
## Example
88+
89+
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.
90+
91+
[`resources.GetRemote`]: /functions/resources/getremote/
92+
93+
Instead of failing the build, we can catch the error and emit a warning:
94+
95+
```go-html-template
96+
{{ $url := "https://broken-example.org/images/a.jpg" }}
97+
{{ with try (resources.GetRemote $url) }}
98+
{{ with .Err }}
99+
{{ warnf "%s" . }}
100+
{{ else with .Value }}
101+
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
102+
{{ else }}
103+
{{ errorf "Unable to get remote resource %q" $url }}
104+
{{ end }}
105+
{{ end }}
106+
```
107+
108+
{{% note %}}
109+
Hugo does not classify an HTTP response with status code 404 as an error. In this case `resources.GetRemote` returns nil.
110+
{{% /note %}}

content/en/functions/images/Text.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ Capture the font as a resource:
4848
```go-html-template
4949
{{ $font := "" }}
5050
{{ $path := "https://github.com/google/fonts/raw/main/ofl/lato/Lato-Regular.ttf" }}
51-
{{ with resources.GetRemote $path }}
51+
{{ with try (resources.GetRemote $path) }}
5252
{{ with .Err }}
5353
{{ errorf "%s" . }}
54-
{{ else }}
54+
{{ else with .Value }}
5555
{{ $font = . }}
56+
{{ else }}
57+
{{ errorf "Unable to get resource %q" $path }}
5658
{{ end }}
57-
{{ else }}
58-
{{ errorf "Unable to get resource %q" $path }}
5959
{{ end }}
6060
```
6161

content/en/functions/openapi3/Unmarshal.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ For example, to work with a remote [OpenAPI] definition:
2222
```go-html-template
2323
{{ $url := "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.json" }}
2424
{{ $api := "" }}
25-
{{ with resources.GetRemote $url }}
25+
{{ with try (resources.GetRemote $url) }}
2626
{{ with .Err }}
2727
{{ errorf "%s" . }}
28-
{{ else }}
28+
{{ else with .Value }}
2929
{{ $api = . | openapi3.Unmarshal }}
30+
{{ else }}
31+
{{ errorf "Unable to get remote resource %q" $url }}
3032
{{ end }}
31-
{{ else }}
32-
{{ errorf "Unable to get remote resource %q" $url }}
3333
{{ end }}
3434
```
3535

0 commit comments

Comments
 (0)