forked from gohugoio/hugoDocs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clarify data type returned by partial and partialCached
- Loading branch information
Showing
4 changed files
with
163 additions
and
12 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
--- | ||
title: Go template functions | ||
title: Go template functions, operators, and statements | ||
linkTitle: go template | ||
description: Template functions and statements provided by Go's text/template package. | ||
description: Template functions, operators, and statements provided by Go's text/template package. | ||
categories: [] | ||
keywords: [] | ||
menu: | ||
docs: | ||
parent: functions | ||
--- | ||
|
||
These are the statements, operators, and functions provided by Go's [text/template] package. | ||
These are the functions, operators, and statements provided by Go's [text/template] package. | ||
|
||
[text/template]: https://pkg.go.dev/text/template |
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: return | ||
description: Used within partial templates, terminates template execution and returns the given value, if any. | ||
categories: [] | ||
keywords: [] | ||
action: | ||
aliases: [] | ||
related: | ||
- functions/partials/Include | ||
- functions/partials/IncludeCached | ||
returnType: any | ||
signatures: ['return [VALUE]'] | ||
toc: true | ||
--- | ||
|
||
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. | ||
|
||
The returned value may be of any data type including, but not limited to, [`bool`], [`float`], [`int`], [`map`], [`resource`], [`slice`], and [`string`]. | ||
|
||
A `return` statement without a value returns an empty string of type `template.HTML`. | ||
|
||
[`bool`]: /getting-started/glossary/#bool | ||
[`float`]: /getting-started/glossary/#float | ||
[`int`]: /getting-started/glossary/#int | ||
[`map`]: /getting-started/glossary/#map | ||
[`resource`]: /getting-started/glossary/#resource | ||
[`slice`]: /getting-started/glossary/#slice | ||
[`string`]: /getting-started/glossary/#string | ||
[text/template]: https://pkg.go.dev/text/template | ||
|
||
{{% note %}} | ||
Unlike `return` statements in other languages, Hugo executes the first occurence of the `return` statement regardless of its position within logical blocks. See [usage](#usage) notes below. | ||
{{% /note %}} | ||
|
||
## Example | ||
|
||
By way of example, let's create a partial template that _renders_ HTML, describing whether the given number is odd or even: | ||
|
||
{{< code file="layouts/partials/odd-or-even.html" >}} | ||
{{ if math.ModBool . 2 }} | ||
<p>{{ . }} is even</p> | ||
{{ else }} | ||
<p>{{ . }} is odd</p> | ||
{{ end }} | ||
{{< /code >}} | ||
|
||
When called, the partial renders HTML: | ||
|
||
```go-html-template | ||
{{ partial "odd-or-even.html" 42 }} → <p>42 is even</p> | ||
``` | ||
|
||
Instead of rendering HTML, let's create a partial that _returns_ a boolean value, reporting whether the given number is even: | ||
|
||
{{< code file="layouts/partials/is-even.html" >}} | ||
{{ return math.ModBool . 2 }} | ||
{{< /code >}} | ||
|
||
With this template: | ||
|
||
```go-html-template | ||
{{ $number := 42 }} | ||
{{ if partial "is-even.html" $number }} | ||
<p>{{ $number }} is even</p> | ||
{{ else }} | ||
<p>{{ $number }} is odd</p> | ||
{{ end }} | ||
``` | ||
|
||
Hugo renders: | ||
|
||
```html | ||
<p>42 is even</p> | ||
``` | ||
|
||
See additional examples in the [partial templates] section. | ||
|
||
[partial templates]: /templates/partials/#returning-a-value-from-a-partial | ||
|
||
## Usage | ||
|
||
{{% note %}} | ||
Unlike `return` statements in other languages, Hugo executes the first occurence of the `return` statement regardless of its position within logical blocks | ||
{{% /note %}} | ||
|
||
A partial that returns a value must contain only one `return` statement, placed at the end of the template. | ||
|
||
For example: | ||
|
||
{{< code file="layouts/partials/is-even.html" >}} | ||
{{ $result := false }} | ||
{{ if math.ModBool . 2 }} | ||
{{ $result = "even" }} | ||
{{ else }} | ||
{{ $result = "odd" }} | ||
{{ end }} | ||
{{ return $result }} | ||
{{< /code >}} | ||
|
||
{{% note %}} | ||
The construct below is incorrect; it contains more than one `return` statement. | ||
{{% /note %}} | ||
|
||
{{< code file="layouts/partials/do-not-do-this.html" >}} | ||
{{ if math.ModBool . 2 }} | ||
{{ return "even" }} | ||
{{ else }} | ||
{{ return "odd" }} | ||
{{ end }} | ||
{{< /code >}} |
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