-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #2808
- Loading branch information
Showing
30 changed files
with
787 additions
and
932 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
--- | ||
title: try | ||
description: Returns a TryValue object after evaluating the given expression. | ||
categories: [] | ||
keywords: [] | ||
action: | ||
aliases: [] | ||
related: [] | ||
returnType: TryValue | ||
signatures: ['try EXPRESSION'] | ||
toc: true | ||
--- | ||
|
||
{{< new-in 0.141.0 >}} | ||
|
||
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. | ||
|
||
[text/template]: https://pkg.go.dev/text/template | ||
|
||
## Methods | ||
|
||
The `TryValue` object encapsulates the result of evaluating the expression, and provides two methods: | ||
|
||
Err | ||
: (`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. | ||
|
||
Value | ||
: (`any`) Returns the result of the expression if the evaluation was successful, or returns `nil` if an error occurred while evaluating the expression. | ||
|
||
## Explanation | ||
|
||
By way of example, let's divide a number by zero: | ||
|
||
```go-html-template | ||
{{ $x := 1 }} | ||
{{ $y := 0 }} | ||
{{ $result := div $x $y }} | ||
{{ printf "%v divided by %v equals %v" $x $y .Value }} | ||
``` | ||
|
||
As expected, the example above throws an error and fails the build: | ||
|
||
```terminfo | ||
Error: error calling div: can't divide the value by 0 | ||
``` | ||
|
||
Instead of failing the build, we can catch the error and emit a warning: | ||
|
||
```go-html-template | ||
{{ $x := 1 }} | ||
{{ $y := 0 }} | ||
{{ with try (div $x $y) }} | ||
{{ with .Err }} | ||
{{ warnf "%s" . }} | ||
{{ else }} | ||
{{ printf "%v divided by %v equals %v" $x $y .Value }} | ||
{{ end }} | ||
{{ end }} | ||
``` | ||
|
||
The error thrown by the expression is logged to the console as a warning: | ||
|
||
```terminfo | ||
WARN error calling div: can't divide the value by 0 | ||
``` | ||
|
||
Now let's change the arguments to avoid dividing by zero: | ||
|
||
```go-html-template | ||
{{ $x := 42 }} | ||
{{ $y := 6 }} | ||
{{ with try (div $x $y) }} | ||
{{ with .Err }} | ||
{{ warnf "%s" . }} | ||
{{ else }} | ||
{{ printf "%v divided by %v equals %v" $x $y .Value }} | ||
{{ end }} | ||
{{ end }} | ||
``` | ||
|
||
Hugo renders the above to: | ||
|
||
```html | ||
42 divided by 6 equals 7 | ||
``` | ||
|
||
## Example | ||
|
||
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. | ||
|
||
[`resources.GetRemote`]: /functions/resources/getremote/ | ||
|
||
Instead of failing the build, we can catch the error and emit a warning: | ||
|
||
```go-html-template | ||
{{ $url := "https://broken-example.org/images/a.jpg" }} | ||
{{ with try (resources.GetRemote $url) }} | ||
{{ with .Err }} | ||
{{ warnf "%s" . }} | ||
{{ else with .Value }} | ||
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt=""> | ||
{{ else }} | ||
{{ errorf "Unable to get remote resource %q" $url }} | ||
{{ end }} | ||
{{ end }} | ||
``` | ||
|
||
{{% note %}} | ||
Hugo does not classify an HTTP response with status code 404 as an error. In this case `resources.GetRemote` returns nil. | ||
{{% /note %}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
title: images.Mask | ||
description: Returns an image filter that applies a mask to the source image. | ||
categories: [] | ||
keywords: [] | ||
action: | ||
aliases: [] | ||
related: | ||
- functions/images/Filter | ||
- methods/resource/Filter | ||
returnType: images.filter | ||
signatures: [images.Mask RESOURCE] | ||
toc: true | ||
--- | ||
|
||
{{< new-in 0.141.0 >}} | ||
|
||
The `images.Mask` filter applies a mask to an image. Black pixels in the mask make the corresponding areas of the base image transparent, while white pixels keep them opaque. Color images are converted to grayscale for masking purposes. The mask is automatically resized to match the dimensions of the base image. | ||
|
||
## Usage | ||
|
||
Create the filter: | ||
|
||
```go-html-template | ||
{{ $filter := images.Mak "images/mask.png" }} | ||
``` | ||
|
||
{{% include "functions/images/_common/apply-image-filter.md" %}} | ||
|
||
## Example | ||
|
||
Mask | ||
|
||
{{< img | ||
src="images/examples/mask.png" | ||
example=false | ||
>}} | ||
{{< img | ||
src="images/examples/zion-national-park.jpg" | ||
alt="Zion National Park" | ||
filter="mask" | ||
filterArgs="images/examples/mask.png" | ||
example=true | ||
>}} |
Oops, something went wrong.