hugo/docs/content/en/functions/templates/Exists.md
2023-10-20 09:43:56 +02:00

931 B

title description categories keywords menu function namespace relatedFunctions aliases
templates.Exists Reports whether a template file exists under the given path relative to the `layouts` directory.
functions
docs
parent
functions
aliases returnType signatures
bool
templates.Exists PATH
templates
/functions/templates.exists

A template file is any file living below the layouts directories of either the project or any of its theme components including partials and shortcodes.

The function is particularly handy with dynamic path. The following example ensures the build will not break on a .Type missing its dedicated header partial.

{{ $partialPath := printf "headers/%s.html" .Type }}
{{ if templates.Exists ( printf "partials/%s" $partialPath ) }}
  {{ partial $partialPath . }}
{{ else }}
  {{ partial "headers/default.html" . }}
{{ end }}