templates.Current
- Syntax
- templates.Current
- Returns
- tpl.CurrentTemplateInfo
Usage
The templates.Current function returns information about the currently executing template. Use it to debug complex template hierarchies and to understand the order of execution during rendering.
Use these methods on the CurrentTemplateInfo object:
Ancestors- (
tpl.CurrentTemplateInfos) Returns a slice containing information about each template in the current execution chain, starting with the parent of the current template and ending with the first template called. The slice excludes any base template applied withdefineandblock. To sort the slice in execution order, chain theReversemethod to the result. Base- (
tpl.CurrentTemplateInfoCommonOps) Returns an object representing the base template applied to the current template, ornilif there is none. Filename- (
string) Returns the absolute path of the current template. The value is empty for embedded templates. Name- (
string) Returns the name of the current template, typically its path relative to thelayoutsdirectory. Parent- (
tpl.CurrentTemplateInfo) Returns an object representing the parent of the current template, ornilif there is none.
Examples
These examples show template execution only when the debug parameter in your project configuration is true:
params:
debug: true
[params]
debug = true
{
"params": {
"debug": true
}
}
Boundaries
Marking where a template begins and ends execution:
{{ define "main" }}
{{ if site.Params.debug }}
<div class="debug">[entering {{ templates.Current.Filename }}]</div>
{{ end }}
<h1>{{ .Title }}</h1>
{{ .Content }}
{{ if site.Params.debug }}
<div class="debug">[leaving {{ templates.Current.Filename }}]</div>
{{ end }}
{{ end }}Call stack
Displaying the chain of templates that led to the current template, using a partial template that iterates through its ancestors:
{{ with templates.Current }}
<div class="debug">
{{ range .Ancestors }}
{{ .Filename }}<br>
{{ with .Base }}
{{ .Filename }}<br>
{{ end }}
{{ end }}
</div>
{{ end }}Calling the partial template from any template:
{{ if site.Params.debug }}
{{ partial "template-call-stack.html" . }}
{{ end }}The resulting output:
/home/user/project/layouts/_partials/footer/copyright.html
/home/user/project/themes/foo/layouts/_partials/footer.html
/home/user/project/layouts/page.html
/home/user/project/themes/foo/layouts/baseof.htmlReversing the order of the entries by chaining the Reverse method to the Ancestors method:
{{ with templates.Current }}
<div class="debug">
{{ range .Ancestors.Reverse }}
{{ with .Base }}
{{ .Filename }}<br>
{{ end }}
{{ .Filename }}<br>
{{ end }}
</div>
{{ end }}VS Code
Rendering links that open the templates in Visual Studio Code, using a partial template with anchor elements that use the vscode URI scheme:
{{ with templates.Current.Parent }}
<div class="debug">
<a href="vscode://file/{{ .Filename }}">{{ .Name }}</a>
{{ with .Base }}
<a href="vscode://file/{{ .Filename }}">{{ .Name }}</a>
{{ end }}
</div>
{{ end }}Calling the partial template from any template:
{{ define "main" }}
<h1>{{ .Title }}</h1>
{{ .Content }}
{{ if site.Params.debug }}
{{ partial "template-open-in-vs-code.html" . }}
{{ end }}
{{ end }}Rendering the entire call stack as links:
{{ with templates.Current }}
<div class="debug">
{{ range .Ancestors }}
<a href="vscode://file/{{ .Filename }}">{{ .Filename }}</a><br>
{{ with .Base }}
<a href="vscode://file/{{ .Filename }}">{{ .Filename }}</a><br>
{{ end }}
{{ end }}
</div>
{{ end }}