hugo/content/en/functions/templates.Exists.md
Bjørn Erik Pedersen b9bd35d72e Squashed 'docs/' content from commit fdea5430f
git-subtree-dir: docs
git-subtree-split: fdea5430f89dfd849d39212abdf5ace0a4763e5a
2019-10-21 10:22:28 +02:00

1 KiB

title linktitle description godocref date publishdate lastmod categories tags menu ns keywords signature workson hugoversion aliases relatedfuncs toc deprecated
templates.Exists Checks whether a template file exists under the given path relative to the `layouts` directory. 2018-11-01 2018-11-01 2018-11-01
functions
docs
parent
functions
templates
template
layouts
templates.Exists PATH
0.46
false false

A template file is any file living below the layouts directories of either the project or any of its theme components incudling 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 }}