templates.Exists

模板檔案是專案或其主題組件的 layouts 目錄中任何檔案。

使用 templates.Exists 函數來檢查動態模板路徑:

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

在上述範例中,如果給定內容類型的 “headers” 部分不存在,Hugo 將回落到一個默認的模板。