AlternativeOutputFormats
Hugo 在構建站點時,會為每個頁面生成一個或多個檔案。例如,在渲染首頁、區段、分類法 和 分類項 頁面時,Hugo 會生成一個 HTML 檔案和一個 RSS 檔案。HTML 和 RSS 都是內建的輸出格式。
您可以建立多種輸出格式,並依據頁面類型,或啟用一個或多個頁面的輸出格式來控制檔案的生成。查看相關 詳細資料。
Page
物件上的 AlternativeOutputFormats
方法會返回一個 OutputFormat
物件的切片,排除當前的輸出格式,每個物件代表為給定頁面啟用的其中一種輸出格式。參見 詳細說明。
方法
- 取得 IDENTIFIER
- (
any
) 會回傳具有給定識別符的OutputFormat
物件。 - MediaType
- (
media.Type
) 回傳輸出格式的媒體類型。 - MediaType.MainType
- (
string
) 回傳輸出格式媒體類型的主要類型。 - MediaType.SubType
- (
string
) 回傳當前格式媒體類型的子類型。 - Name
- (
string
) 回傳輸出格式的識別名稱。 - Permalink
- (
string
) 回傳由當前輸出格式生成的頁面的永久鏈接。 - Rel
- (
string
) 回傳輸出格式的rel
值,可以是預設值或依站點設定定義的值。 - RelPermalink
- (
string
) 回傳由當前輸出格式生成的頁面的相對永久鏈接。
範例
在每個頁面的 <head>
中生成一個 link
元素,用於每個替代輸出格式:
<head>
...
{{ $title := printf "%s | %s" .Title site.Title }}
{{ if .IsHome }}
{{ $title = site.Title }}
{{ end }}
{{ range .AlternativeOutputFormats -}}
{{ printf `<link rel=%q type=%q href=%q title=%q>` .Rel .MediaType.Type .Permalink $title | safeHTML }}
{{ end }}
...
</head>
在網站的首頁,Hugo 會渲染如下內容:
<link rel="alternate" type="application/rss+xml" href="https://example.org/index.xml" title="ABC Widgets, Inc.">