Current.md - hugo - [fork] hugo port for 9front
HTML git clone https://git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
Current.md (4735B)
---
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 Ancestors
23 : (`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.
24
25 Base
26 : (`tpl.CurrentTemplateInfoCommonOps`) Returns an object representing the base template that was applied to the current template, if any. This may be `nil`.
27
28 Filename
29 : (`string`) Returns the absolute path of the current template. This will be empty for embedded templates.
30
31 Name
32 : (`string`) Returns the name of the current template. This is usually the path relative to the layouts directory.
33
34 Parent
35 : (`tpl.CurrentTemplateInfo`) Returns an object representing the parent of the current template, if any. This may be `nil`.
36
37 ## Examples
38
39 The examples below help visualize template execution and require a `debug` parameter set to `true` in your site configuration:
40
41 {{< code-toggle file=hugo >}}
42 [params]
43 debug = true
44 {{< /code-toggle >}}
45
46 ### Boundaries
47
48 To visually mark where a template begins and ends execution:
49
50 ```go-html-template {file="layouts/page.html"}
51 {{ define "main" }}
52 {{ if site.Params.debug }}
53 <div class="debug">[entering {{ templates.Current.Filename }}]</div>
54 {{ end }}
55
56 <h1>{{ .Title }}</h1>
57 {{ .Content }}
58
59 {{ if site.Params.debug }}
60 <div class="debug">[leaving {{ templates.Current.Filename }}]</div>
61 {{ end }}
62 {{ end }}
63 ```
64
65 ### Call stack
66
67 To display the chain of templates that led to the current one, create a partial template that iterates through its ancestors:
68
69 ```go-html-template {file="layouts/_partials/template-call-stack.html" copy=true}
70 {{ with templates.Current }}
71 <div class="debug">
72 {{ range .Ancestors }}
73 {{ .Filename }}<br>
74 {{ with .Base }}
75 {{ .Filename }}<br>
76 {{ end }}
77 {{ end }}
78 </div>
79 {{ end }}
80 ```
81
82 Then call the partial from any template:
83
84 ```go-html-template {file="layouts/_partials/footer/copyright.html" copy=true}
85 {{ if site.Params.debug }}
86 {{ partial "template-call-stack.html" . }}
87 {{ end }}
88 ```
89
90 The rendered template stack would look something like this:
91
92 ```text
93 /home/user/project/layouts/_partials/footer/copyright.html
94 /home/user/project/themes/foo/layouts/_partials/footer.html
95 /home/user/project/layouts/page.html
96 /home/user/project/themes/foo/layouts/baseof.html
97 ```
98
99 To reverse the order of the entries, chain the `Reverse` method to the `Ancestors` method:
100
101 ```go-html-template {file="layouts/_partials/template-call-stack.html" copy=true}
102 {{ with templates.Current }}
103 <div class="debug">
104 {{ range .Ancestors.Reverse }}
105 {{ with .Base }}
106 {{ .Filename }}<br>
107 {{ end }}
108 {{ .Filename }}<br>
109 {{ end }}
110 </div>
111 {{ end }}
112 ```
113
114 ### VS Code
115
116 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:
117
118 ```go-html-template {file="layouts/_partials/template-open-in-vs-code.html" copy=true}
119 {{ with templates.Current.Parent }}
120 <div class="debug">
121 <a href="vscode://file/{{ .Filename }}">{{ .Name }}</a>
122 {{ with .Base }}
123 <a href="vscode://file/{{ .Filename }}">{{ .Name }}</a>
124 {{ end }}
125 </div>
126 {{ end }}
127 ```
128
129 Then call the partial from any template:
130
131 ```go-html-template {file="layouts/page.html" copy=true}
132 {{ define "main" }}
133 <h1>{{ .Title }}</h1>
134 {{ .Content }}
135
136 {{ if site.Params.debug }}
137 {{ partial "template-open-in-vs-code.html" . }}
138 {{ end }}
139 {{ end }}
140 ```
141
142 Use the same approach to render the entire call stack as links:
143
144 ```go-html-template {file="layouts/_partials/template-call-stack.html" copy=true}
145 {{ with templates.Current }}
146 <div class="debug">
147 {{ range .Ancestors }}
148 <a href="vscode://file/{{ .Filename }}">{{ .Filename }}</a><br>
149 {{ with .Base }}
150 <a href="vscode://file/{{ .Filename }}">{{ .Filename }}</a><br>
151 {{ end }}
152 {{ end }}
153 </div>
154 {{ end }}
155 ```