# templates.Exists
> 报告相对于 `layouts` 目录的给定路径下是否存在模板文件。
- 官方英文原文：https://gohugo.io/functions/templates/exists/
- 本页规范地址：https://hugozh.cn/functions/templates/exists/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：templates.Exists PATH
- 返回类型：bool
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
模板文件是指项目自身或其任一主题组件 `layouts` 目录中的任何文件。

把 `templates.Exists` 函数用于动态模板路径：

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

在上例中，如果给定内容类型没有对应的 "headers" _partial_ 模板，Hugo 会回退到默认模板。

