Welcome to mirror list, hosted at ThFree Co, Russian Federation.

templates.Exists.md « functions « en « content « docs - github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3c8c9615609a0b0081c83e87eacd880750b19a73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
---
title: templates.Exists
linktitle: ""
description: "Checks whether a template file exists under the given path relative to the `layouts` directory."
godocref: ""
date: 2018-11-01
publishdate: 2018-11-01
lastmod: 2018-11-01
categories: [functions]
tags: []
menu:
  docs:
    parent: "functions"
ns: ""
keywords: ["templates", "template", "layouts"]
signature: ["templates.Exists PATH"]
workson: []
hugoversion: "0.46"
aliases: []
relatedfuncs: []
toc: false
deprecated: 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.

```go-html-template
{{ $partialPath := printf "headers/%s.html" .Type }}
{{ if templates.Exists ( printf "partials/%s" $partialPath ) }}
  {{ partial $partialPath . }}
{{ else }}
  {{ partial "headers/default.html" . }}
{{ end }}

```