Debian Codegraph @

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

raw
1 ---
2 title: templates.Current
3 description: Returns information about the currently executing template.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: []
9 returnType: tpl.CurrentTemplateInfo
10 signatures: [templates.Current]
11 ---
12
13 > [!NOTE]
14 > This function is experimental and subject to change.
15
16 {{< new-in 0.146.0 />}}
17
18 The `templates.Current` function provides introspection capabilities, allowing you to access details about the currently executing templates. This is useful for debugging complex template hierarchies and understanding the flow of execution during rendering.
19
20 ## Methods
21
22 Use these methods on the `CurrentTemplateInfo` object.
23
24 `Ancestors`
25 : (`tpl.CurrentTemplateInfos`) Returns a slice containing information about each template in the current execution chain, starting from the parent of the current template and going up towards the initial template called. It excludes any _base_ template applied via `define` and `block`. You can chain the `Reverse` method to this result to get the slice in chronological execution order.
26
27 `Base`
28 : (`tpl.CurrentTemplateInfoCommonOps`) Returns an object representing the _base_ template that was applied to the current template, if any. This may be `nil`.
29
30 `Filename`
31 : (`string`) Returns the absolute path of the current template. This will be empty for embedded templates.
32
33 `Name`
34 : (`string`) Returns the name of the current template. This is usually the path relative to the layouts directory.
35
36 `Parent`
37 : (`tpl.CurrentTemplateInfo`) Returns an object representing the parent of the current template, if any. This may be `nil`.
38
39 ## Examples
40
41 The examples below help visualize template execution and require a `debug` parameter set to `true` in your project configuration:
42
43 {{< code-toggle file=hugo >}}
44 [params]
45 debug = true
46 {{< /code-toggle >}}
47
48 ### Boundaries
49
50 To visually mark where a template begins and ends execution:
51
52 ```go-html-template {file="layouts/page.html"}
53 {{ define "main" }}
54 {{ if site.Params.debug }}
55 <div class="debug">[entering {{ templates.Current.Filename }}]</div>
56 {{ end }}
57
58 <h1>{{ .Title }}</h1>
59 {{ .Content }}
60
61 {{ if site.Params.debug }}
62 <div class="debug">[leaving {{ templates.Current.Filename }}]</div>
63 {{ end }}
64 {{ end }}
65 ```
66
67 ### Call stack
68
69 To display the chain of templates that led to the current one, create a _partial_ template that iterates through its ancestors:
70
71 ```go-html-template {file="layouts/_partials/template-call-stack.html" copy=true}
72 {{ with templates.Current }}
73 <div class="debug">
74 {{ range .Ancestors }}
75 {{ .Filename }}<br>
76 {{ with .Base }}
77 {{ .Filename }}<br>
78 {{ end }}
79 {{ end }}
80 </div>
81 {{ end }}
82 ```
83
84 Then call the _partial_ template from any template:
85
86 ```go-html-template {file="layouts/_partials/footer/copyright.html" copy=true}
87 {{ if site.Params.debug }}
88 {{ partial "template-call-stack.html" . }}
89 {{ end }}
90 ```
91
92 The rendered template stack would look something like this:
93
94 ```text
95 /home/user/project/layouts/_partials/footer/copyright.html
96 /home/user/project/themes/foo/layouts/_partials/footer.html
97 /home/user/project/layouts/page.html
98 /home/user/project/themes/foo/layouts/baseof.html
99 ```
100
101 To reverse the order of the entries, chain the `Reverse` method to the `Ancestors` method:
102
103 ```go-html-template {file="layouts/_partials/template-call-stack.html" copy=true}
104 {{ with templates.Current }}
105 <div class="debug">
106 {{ range .Ancestors.Reverse }}
107 {{ with .Base }}
108 {{ .Filename }}<br>
109 {{ end }}
110 {{ .Filename }}<br>
111 {{ end }}
112 </div>
113 {{ end }}
114 ```
115
116 ### VS Code
117
118 To render links that, when clicked, will open the template in Microsoft Visual Studio Code, create a _partial_ template with anchor elements that use the `vscode` URI scheme:
119
120 ```go-html-template {file="layouts/_partials/template-open-in-vs-code.html" copy=true}
121 {{ with templates.Current.Parent }}
122 <div class="debug">
123 <a href="vscode://file/{{ .Filename }}">{{ .Name }}</a>
124 {{ with .Base }}
125 <a href="vscode://file/{{ .Filename }}">{{ .Name }}</a>
126 {{ end }}
127 </div>
128 {{ end }}
129 ```
130
131 Then call the _partial_ template from any template:
132
133 ```go-html-template {file="layouts/page.html" copy=true}
134 {{ define "main" }}
135 <h1>{{ .Title }}</h1>
136 {{ .Content }}
137
138 {{ if site.Params.debug }}
139 {{ partial "template-open-in-vs-code.html" . }}
140 {{ end }}
141 {{ end }}
142 ```
143
144 Use the same approach to render the entire call stack as links:
145
146 ```go-html-template {file="layouts/_partials/template-call-stack.html" copy=true}
147 {{ with templates.Current }}
148 <div class="debug">
149 {{ range .Ancestors }}
150 <a href="vscode://file/{{ .Filename }}">{{ .Filename }}</a><br>
151 {{ with .Base }}
152 <a href="vscode://file/{{ .Filename }}">{{ .Filename }}</a><br>
153 {{ end }}
154 {{ end }}
155 </div>
156 {{ end }}
157 ```