templates.Exists
Reports whether a template file exists under the given path relative to the layouts directory.
Syntax
templates.Exists PATH
Returns
bool
A template file is any file within the layouts
directory of either the project or any of its theme components.
Use the templates.Exists
function with dynamic template paths:
{{ $partialPath := printf "headers/%s.html" .Type }}
{{ if templates.Exists ( printf "partials/%s" $partialPath ) }}
{{ partial $partialPath . }}
{{ else }}
{{ partial "headers/default.html" . }}
{{ end }}
In the example above, if a “headers” partial does not exist for the given content type, Hugo falls back to a default template.
Last updated:
December 4, 2023
:
Squashed 'docs/' changes from 4d936aee6..4dd2d6415 (35dec7c96)
Improve this page