mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-19 21:21:39 +02:00
Merge commit 'e509cac533600cf4fa8382c9cdab78ddd82db688'
This commit is contained in:
29
docs/content/en/functions/templates/Exists.md
Normal file
29
docs/content/en/functions/templates/Exists.md
Normal file
@@ -0,0 +1,29 @@
|
||||
---
|
||||
title: templates.Exists
|
||||
description: Reports whether a template file exists under the given path relative to the `layouts` directory.
|
||||
categories: [functions]
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
function:
|
||||
aliases: []
|
||||
returnType: bool
|
||||
signatures: [templates.Exists PATH]
|
||||
namespace: templates
|
||||
relatedFunctions: []
|
||||
aliases: [/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.
|
||||
|
||||
```go-html-template
|
||||
{{ $partialPath := printf "headers/%s.html" .Type }}
|
||||
{{ if templates.Exists ( printf "partials/%s" $partialPath ) }}
|
||||
{{ partial $partialPath . }}
|
||||
{{ else }}
|
||||
{{ partial "headers/default.html" . }}
|
||||
{{ end }}
|
||||
```
|
Reference in New Issue
Block a user