Debian Codegraph @

docs/content/en/functions/templates/Inner.md

raw
1 ---
2 title: templates.Inner
3 description: Executes the content block enclosed by a partial call.
4 categories: []
5 keywords: [decorator]
6 params:
7 functions_and_methods:
8 aliases: [inner]
9 returnType: any
10 signatures: ['templates.Inner [CONTEXT]']
11 ---
12
13 {{< new-in 0.154.0 />}}
14
15 The `templates.Inner` function defines the injection point for code nested within a block style _partial_ template call. This is the core mechanism used to create a [partial decorator][].
16
17 ## Overview
18
19 The `templates.Inner` function acts as a placeholder within a _partial_ template. When a _partial_ template is called as a decorator, it captures a block of code from the calling template rather than rendering it immediately. The `templates.Inner` function tells Hugo exactly where to inject that captured content.
20
21 This signals a reversal of execution where the callee becomes the caller. The _partial_ template manages the outer structure while the calling template remains in control of the inner content.
22
23 ## Usage
24
25 To use this function, the calling template must use the block style syntax with a [`with`][] statement. This allows decorators to be deeply nested.
26
27 ```go-html-template {file="layouts/home.html"}
28 {{ with partial "components/card.html" . }}
29 <p>This content is passed to the partial.</p>
30 {{ end }}
31 ```
32
33 Inside the _partial_ template, call `templates.Inner` to render the captured block.
34
35 ```go-html-template {file="layouts/_partials/components/card.html"}
36 <div class="card-frame">
37 {{ templates.Inner . }}
38 </div>
39 ```
40
41 ## Arguments
42
43 The function accepts one optional argument: the [context](g). This argument determines the value of the dot (`.`) inside the captured block when it is rendered.
44
45 - If you provide an argument, such as `{{ templates.Inner .SomeData }}`, the dot inside the captured block is rebound to that specific data.
46 - If you do not provide an argument, the captured block uses the context of the caller where the _partial_ template was first invoked.
47
48 ## Context and scope
49
50 When using decorators, the `with` statement creates a new [scope](g). Variables defined outside the with block in the calling template are not automatically available inside the captured block.
51
52 By passing a context to `templates.Inner`, you ensure that the injected content has access to the correct data even when nested inside multiple layers of wrappers. This is critical when the decorator is used inside a loop or a specific data overlay.
53
54 ## Repeated execution
55
56 A decorator can execute the captured content zero or more times. This is useful when the wrapper needs to repeat the same decoration for a collection of items, such as a list or a grid.
57
58 ```go-html-template {file="layouts/_partials/list-decorator.html"}
59 <ul class="styled-list">
60 {{ range .items }}
61 <li>
62 {{ templates.Inner . }}
63 </li>
64 {{ end }}
65 </ul>
66 ```
67
68 In this example, the code provided by the caller is rendered once for every item in the .items collection, with the dot . updated to the current item in each iteration.
69
70 [`with`]: /functions/go-template/with/
71 [partial decorator]: /templates/partial-decorators/