mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-22 21:42:50 +02:00
Merge commit '9b0050e9aabe4be65c78ccf292a348f309d50ccd' as 'docs'
``` git subtree add --prefix=docs/ https://github.com/gohugoio/hugoDocs.git master --squash ``` Closes #11925
This commit is contained in:
85
docs/content/en/functions/partials/Include.md
Normal file
85
docs/content/en/functions/partials/Include.md
Normal file
@@ -0,0 +1,85 @@
|
||||
---
|
||||
title: partials.Include
|
||||
description: Executes the given partial template, optionally passing context. If the partial template contains a return statement, returns the given value, else returns the rendered output.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [partial]
|
||||
related:
|
||||
- functions/go-template/return
|
||||
- functions/partials/IncludeCached
|
||||
- functions/go-template/template
|
||||
- methods/page/Render
|
||||
returnType: any
|
||||
signatures: ['partials.Include NAME [CONTEXT]']
|
||||
aliases: [/functions/partial]
|
||||
---
|
||||
|
||||
Without a [`return`] statement, the `partial` function returns a string of type `template.HTML`. With a `return` statement, the `partial` function can return any data type.
|
||||
|
||||
[`return`]: /functions/go-template/return
|
||||
|
||||
In this example we have three partial templates:
|
||||
|
||||
```text
|
||||
layouts/
|
||||
└── partials/
|
||||
├── average.html
|
||||
├── breadcrumbs.html
|
||||
└── footer.html
|
||||
```
|
||||
|
||||
The "average" partial returns the average of one or more numbers. We pass the numbers in context:
|
||||
|
||||
```go-html-template
|
||||
{{ $numbers := slice 1 6 7 42 }}
|
||||
{{ $average := partial "average.html" $numbers }}
|
||||
```
|
||||
|
||||
The "breadcrumbs" partial renders [breadcrumb navigation], and needs to receive the current page in context:
|
||||
|
||||
```go-html-template
|
||||
{{ partial "breadcrumbs.html" . }}
|
||||
```
|
||||
|
||||
The "footer" partial renders the site footer. In this contrived example, the footer does not need access to the current page, so we can omit context:
|
||||
|
||||
```go-html-template
|
||||
{{ partial "breadcrumbs.html" }}
|
||||
```
|
||||
|
||||
You can pass anything in context: a page, a page collection, a scalar value, a slice, or a map. For example:
|
||||
|
||||
```go-html-template
|
||||
{{ $student := dict
|
||||
"name" "John Doe"
|
||||
"major" "Finance"
|
||||
"gpa" 4.0
|
||||
}}
|
||||
{{ partial "render-student-info.html" $student }}
|
||||
```
|
||||
|
||||
Then, within the partial template:
|
||||
|
||||
```go-html-template
|
||||
<p>{{ .name }} is majoring in {{ .major }}. Their grade point average is {{ .gpa }}.</p>
|
||||
```
|
||||
|
||||
To return a value from a partial template, it must contain only one `return` statement, placed at the end of the template:
|
||||
|
||||
```go-html-template
|
||||
{{ $result := "" }}
|
||||
{{ if math.ModBool . 2 }}
|
||||
{{ $result = "even" }}
|
||||
{{ else }}
|
||||
{{ $result = "odd" }}
|
||||
{{ end }}
|
||||
{{ return $result }}
|
||||
```
|
||||
|
||||
See [details][`return`].
|
||||
|
||||
[`return`]: /functions/go-template/return
|
||||
|
||||
[breadcrumb navigation]: /content-management/sections/#ancestors-and-descendants
|
||||
[details]: /functions/go-template/return
|
65
docs/content/en/functions/partials/IncludeCached.md
Normal file
65
docs/content/en/functions/partials/IncludeCached.md
Normal file
@@ -0,0 +1,65 @@
|
||||
---
|
||||
title: partials.IncludeCached
|
||||
description: Executes the given template and caches the result, optionally passing context. If the partial template contains a return statement, returns the given value, else returns the rendered output.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [partialCached]
|
||||
related:
|
||||
- functions/go-template/return
|
||||
- functions/partials/Include
|
||||
- functions/go-template/template
|
||||
- methods/page/Render
|
||||
returnType: any
|
||||
signatures: ['partials.IncludeCached LAYOUT CONTEXT [VARIANT...]']
|
||||
signatures:
|
||||
- partials.IncludeCached NAME CONTEXT [VARIANT...]
|
||||
- partialCached NAME CONTEXT [VARIANT...]
|
||||
aliases: [/functions/partialcached]
|
||||
---
|
||||
|
||||
Without a [`return`] statement, the `partialCached` function returns a string of type `template.HTML`. With a `return` statement, the `partialCached` function can return any data type.
|
||||
|
||||
The `partialCached` function can offer significant performance gains for complex templates that don't need to be re-rendered on every invocation.
|
||||
|
||||
{{% note %}}
|
||||
Each Site (or language) has its own `partialCached` cache, so each site will execute a partial once.
|
||||
|
||||
Hugo renders pages in parallel, and will render the partial more than once with concurrent calls to the `partialCached` function. After Hugo caches the rendered partial, new pages entering the build pipeline will use the cached result.
|
||||
{{% /note %}}
|
||||
|
||||
Here is the simplest usage:
|
||||
|
||||
```go-html-template
|
||||
{{ partialCached "footer.html" . }}
|
||||
```
|
||||
|
||||
Pass additional arguments to `partialCached` to create variants of the cached partial. For example, if you have a complex partial that should be identical when rendered for pages within the same section, use a variant based on section so that the partial is only rendered once per section:
|
||||
|
||||
{{< code file=partial-cached-example.html >}}
|
||||
{{ partialCached "footer.html" . .Section }}
|
||||
{{< /code >}}
|
||||
|
||||
Pass additional arguments, of any data type, as needed to create unique variants:
|
||||
|
||||
```go-html-template
|
||||
{{ partialCached "footer.html" . .Params.country .Params.province }}
|
||||
```
|
||||
|
||||
The variant arguments are not available to the underlying partial template; they are only used to create unique cache keys.
|
||||
|
||||
To return a value from a partial template, it must contain only one `return` statement, placed at the end of the template:
|
||||
|
||||
```go-html-template
|
||||
{{ $result := "" }}
|
||||
{{ if math.ModBool . 2 }}
|
||||
{{ $result = "even" }}
|
||||
{{ else }}
|
||||
{{ $result = "odd" }}
|
||||
{{ end }}
|
||||
{{ return $result }}
|
||||
```
|
||||
|
||||
See [details][`return`].
|
||||
|
||||
[`return`]: /functions/go-template/return
|
12
docs/content/en/functions/partials/_index.md
Normal file
12
docs/content/en/functions/partials/_index.md
Normal file
@@ -0,0 +1,12 @@
|
||||
---
|
||||
title: Partial functions
|
||||
linkTitle: partials
|
||||
description: Template functions to call partial templates.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
---
|
||||
|
||||
Use these functions to call partial templates.
|
Reference in New Issue
Block a user