| 1 |
--- |
| 2 |
title: templates.Exists |
| 3 |
description: Reports whether a template file exists under the given path relative to the layouts directory. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: bool |
| 10 |
signatures: [templates.Exists PATH] |
| 11 |
aliases: [/functions/templates.exists] |
| 12 |
--- |
| 13 |
|
| 14 |
A template file is any file within the `layouts` directory of either the project or any of its theme components. |
| 15 |
|
| 16 |
Use the `templates.Exists` function with dynamic template paths: |
| 17 |
|
| 18 |
```go-html-template |
| 19 |
{{ $partialPath := printf "headers/%s.html" .Type }} |
| 20 |
{{ if templates.Exists ( printf "_partials/%s" $partialPath ) }} |
| 21 |
{{ partial $partialPath . }} |
| 22 |
{{ else }} |
| 23 |
{{ partial "headers/default.html" . }} |
| 24 |
{{ end }} |
| 25 |
``` |
| 26 |
|
| 27 |
In the example above, if a "headers" partial does not exist for the given content type, Hugo falls back to a default template. |