mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-31 22:41:53 +02:00
Merge commit 'a0c28c943c2f4714fa340b22a583b96f5013090b'
This commit is contained in:
@@ -77,7 +77,47 @@ The second argument in a partial call is the variable being passed down. The abo
|
||||
|
||||
This means the partial will *only* be able to access those variables. The partial is isolated and *has no access to the outer scope*. From within the partial, `$.Var` is equivalent to `.Var`.
|
||||
|
||||
### Cached Partials
|
||||
## Returning a value from a Partial
|
||||
|
||||
In addition to outputting markup, partials can be used to return a value of any type. In order to return a value, a partial must include a lone `return` statement.
|
||||
|
||||
### Example GetFeatured
|
||||
```go-html-template
|
||||
{{/* layouts/partials/GetFeatured.html */}}
|
||||
{{ return first . (where site.RegularPages ".Params.featured" true) }}
|
||||
```
|
||||
|
||||
```go-html-template
|
||||
{{/* layouts/index.html */}}
|
||||
{{ range partial "GetFeatured.html" 5 }}
|
||||
[...]
|
||||
{{ end }}
|
||||
```
|
||||
### Example GetImage
|
||||
```go-html-template
|
||||
{{/* layouts/partials/GetImage.html */}}
|
||||
{{ $image := false }}
|
||||
{{ with .Params.gallery }}
|
||||
{{ $image = index . 0 }}
|
||||
{{ end }}
|
||||
{{ with .Params.image }}
|
||||
{{ $image = . }}
|
||||
{{ end }}
|
||||
{{ return $image }}
|
||||
```
|
||||
|
||||
```go-html-template
|
||||
{{/* layouts/_default/single.html */}}
|
||||
{{ with partial "GetImage.html" . }}
|
||||
[...]
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
{{% note %}}
|
||||
Only one `return` statement is allowed per partial file.
|
||||
{{% /note %}}
|
||||
|
||||
## Cached Partials
|
||||
|
||||
The [`partialCached` template function][partialcached] can offer significant performance gains for complex templates that don't need to be re-rendered on every invocation. The simplest usage is as follows:
|
||||
|
||||
|
Reference in New Issue
Block a user