templates.Exists.md (1025B)
1 --- 2 title: templates.Exists 3 linktitle: "" 4 description: "Checks whether a template file exists under the given path relative to the `layouts` directory." 5 date: 2018-11-01 6 publishdate: 2018-11-01 7 lastmod: 2018-11-01 8 categories: [functions] 9 tags: [] 10 menu: 11 docs: 12 parent: "functions" 13 ns: "" 14 keywords: ["templates", "template", "layouts"] 15 signature: ["templates.Exists PATH"] 16 workson: [] 17 hugoversion: "0.46" 18 aliases: [] 19 relatedfuncs: [] 20 toc: false 21 deprecated: false 22 --- 23 24 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. 25 26 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. 27 28 ```go-html-template 29 {{ $partialPath := printf "headers/%s.html" .Type }} 30 {{ if templates.Exists ( printf "partials/%s" $partialPath ) }} 31 {{ partial $partialPath . }} 32 {{ else }} 33 {{ partial "headers/default.html" . }} 34 {{ end }} 35 36 ```