OutputFormats
返回 OutputFormat 对象的切片,每个代表为给定页面启用的输出格式之一。
Syntax
PAGE.OutputFormats
Returns
[]OutputFormat
output format(输出格式)是一组设置,定义 Hugo 在构建网站时如何渲染文件。例如,html、json ?rss 是内置输出格式。您可以创建多个输出格式,并根据 page kind(页面类型)控制它们的生成,或通过为特定页面启用一个或多个输出格式
Page 对象上的 OutputFormats 方法返回 OutputFormat 对象的切片,每个代表为给定页面启用的输出格式之一。详见 详情。
方法
Canonical
New in v0.154.4(page.OutputFormat) 返回当前页面的 规范输出格式(如果已定义)。一旦捕获了对象,就可以使用其任何 关联方法。
{{ with .Site.Home.OutputFormats.Canonical }}
{{ .MediaType.Type }} → text/html
{{ .MediaType.MainType }} → text
{{ .MediaType.SubType }} → html
{{ .Name }} → html
{{ .Permalink }} → https://example.org/
{{ .Rel }} → canonical
{{ .RelPermalink }} → /
{{ end }}Get
(page.OutputFormat) 返回具有给定标识符的 OutputFormat 对象。一旦捕获了对象,就可以使用其任何 关联方法。
{{ with .Site.Home.OutputFormats.Get "rss" }}
{{ .MediaType.Type }} → application/rss+xml
{{ .MediaType.MainType }} → application
{{ .MediaType.SubType }} → rss
{{ .Name }} → rss
{{ .Permalink }} → https://example.org/index.xml
{{ .Rel }} → alternate
{{ .RelPermalink }} → /index.xml
{{ end }}示例
渲染指向当前页面 规范输出格式 的 link 元素:
{{ with .OutputFormats.Canonical }}
{{ printf "<link rel=%q type=%q href=%q>" .Rel .MediaType.Type .Permalink | safeHTML }}
{{ end }}渲染指向当前页面 rss 输出格式的锚元素:
{{ with .OutputFormats.Get "rss" }}
<a href="{{ .RelPermalink }}">RSS Feed</a>
{{ end }}请查看 链接到输出格式 部分以了解上述构造的重要性。