hugo/docs/content/en/functions/templates/Exists.md
Bjørn Erik Pedersen 5fd1e74903
Merge commit '9b0050e9aabe4be65c78ccf292a348f309d50ccd' as 'docs'
```
git subtree add --prefix=docs/ https://github.com/gohugoio/hugoDocs.git master --squash
```

Closes #11925
2024-01-27 10:48:57 +01:00

843 B

title description categories keywords action aliases
templates.Exists Reports whether a template file exists under the given path relative to the layouts directory.
aliases related returnType signatures
bool
templates.Exists PATH
/functions/templates.exists

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.