mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-22 21:42:50 +02:00
Merge commit 'e509cac533600cf4fa8382c9cdab78ddd82db688'
This commit is contained in:
67
docs/content/en/functions/partials/Include.md
Normal file
67
docs/content/en/functions/partials/Include.md
Normal file
@@ -0,0 +1,67 @@
|
||||
---
|
||||
title: partials.Include
|
||||
linkTitle: partial
|
||||
description: Executes the named partial template. If the partial contains a return statement, returns that value, else returns the rendered output.
|
||||
categories: [functions]
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
aliases: [partial]
|
||||
returnType: any
|
||||
signatures: ['partials.Include LAYOUT [CONTEXT]']
|
||||
relatedFunctions:
|
||||
- partials.Include
|
||||
- partials.IncludeCached
|
||||
aliases: [/functions/partial]
|
||||
---
|
||||
|
||||
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>
|
||||
```
|
||||
|
||||
|
||||
[breadcrumb navigation]: /content-management/sections/#ancestors-and-descendants
|
49
docs/content/en/functions/partials/IncludeCached.md
Normal file
49
docs/content/en/functions/partials/IncludeCached.md
Normal file
@@ -0,0 +1,49 @@
|
||||
---
|
||||
title: partials.IncludeCached
|
||||
linkTitle: partialCached
|
||||
description: Allows for caching of partials that do not need to be re-rendered on every invocation.
|
||||
categories: [functions]
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
aliases: [partialCached]
|
||||
returnType: any
|
||||
signatures: ['partials.IncludeCached LAYOUT CONTEXT [VARIANT...]']
|
||||
relatedFunctions:
|
||||
- partials.Include
|
||||
- partials.IncludeCached
|
||||
signatures:
|
||||
- partials.IncludeCached LAYOUT CONTEXT [VARIANT...]
|
||||
- partialCached LAYOUT CONTEXT [VARIANT...]
|
||||
aliases: [/functions/partialcached]
|
||||
---
|
||||
|
||||
The `partialCached` template 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.
|
||||
|
||||
**Note**: 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.
|
||||
|
||||
Here is the simplest usage:
|
||||
|
||||
```go-html-template
|
||||
{{ partialCached "footer.html" . }}
|
||||
```
|
||||
|
||||
You can also 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, you could use a variant based upon section so that the partial is only rendered once per section:
|
||||
|
||||
{{< code file="partial-cached-example.html" >}}
|
||||
{{ partialCached "footer.html" . .Section }}
|
||||
{{< /code >}}
|
||||
|
||||
If you need to pass additional arguments to create unique variants, you can pass as many variant arguments as you need:
|
||||
|
||||
```go-html-template
|
||||
{{ partialCached "footer.html" . .Params.country .Params.province }}
|
||||
```
|
||||
|
||||
Note that the variant arguments are not made available to the underlying partial template. They are only use to create a unique cache key. Since Hugo `0.61.0` you can use any object as cache key(s), not just strings.
|
||||
|
||||
See also [The Full Partial Series Part 1: Caching!](https://regisphilibert.com/blog/2019/12/hugo-partial-series-part-1-caching-with-partialcached/).
|
Reference in New Issue
Block a user