Debian Codegraph @

docs/content/en/functions/partials/IncludeCached.md

raw
1 ---
2 title: partials.IncludeCached
3 description: Executes the given template and caches the result, optionally passing one or more variant keys. If the partial template contains a return statement, returns the given value, else returns the rendered output.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: [partialCached]
9 returnType: any
10 signatures: ['partials.IncludeCached LAYOUT CONTEXT [VARIANT...]']
11 aliases: [/functions/partialcached]
12 ---
13
14 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.
15
16 The `partialCached` function can offer significant performance gains for complex templates that don't need to be re-rendered on every invocation.
17
18 > [!NOTE]
19 > Each site (or language) has its own `partialCached` cache, so each site will execute a partial once.
20 >
21 > 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.
22
23 Here is the simplest usage:
24
25 ```go-html-template
26 {{ partialCached "footer.html" . }}
27 ```
28
29 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:
30
31 ```go-html-template {file="layouts/baseof.html"}
32 {{ partialCached "footer.html" . .Section }}
33 ```
34
35 Pass additional arguments, of any data type, as needed to create unique variants:
36
37 ```go-html-template
38 {{ partialCached "footer.html" . .Params.country .Params.province }}
39 ```
40
41 The variant arguments are not available to the underlying _partial_ template; they are only used to create unique cache keys.
42
43 To return a value from a _partial_ template, it must contain only one `return` statement, placed at the end of the template:
44
45 ```go-html-template
46 {{ $result := "" }}
47 {{ if math.ModBool . 2 }}
48 {{ $result = "even" }}
49 {{ else }}
50 {{ $result = "odd" }}
51 {{ end }}
52 {{ return $result }}
53 ```
54
55 [`return`]: /functions/go-template/return/