# OutputFormats
> 返回一组 OutputFormat 对象，每个对象对应该页面已启用的一种输出格式。
- 官方英文原文：https://gohugo.io/methods/page/outputformats/
- 本页规范地址：https://hugozh.cn/methods/page/outputformats/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：PAGE.OutputFormats
- 返回类型：[]OutputFormat
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
[输出格式（output format）](/quick-reference/glossary/output-format/)

`Page` 对象上的 `OutputFormats` 方法返回一组 `OutputFormat` 对象，每个对象对应该页面已启用的一种输出格式。详见[说明][]。

## 方法

在 `OutputFormats` 对象上使用这些方法。

`Canonical`
: **（0.154.4 新增）**
: （`page.OutputFormat`）返回当前页面的[规范输出格式](g)（如果已定义）。获取该对象后，可以使用它的任意[关联方法][]。

  ```go-html-template
  {{ 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` 对象。获取该对象后，可以使用它的任意[关联方法][]。

  ```go-html-template
  {{ 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 }}
  ```

## 示例

要渲染指向当前页面[规范输出格式](g)的 `link` 元素：

```go-html-template
{{ with .OutputFormats.Canonical }}
  {{ printf "<link rel=%q type=%q href=%q>" .Rel .MediaType.Type .Permalink | safeHTML }}
{{ end }}
```

要渲染指向当前页面 `rss` 输出格式的锚点元素：

```go-html-template
{{ with .OutputFormats.Get "rss" }}
  <a href="{{ .RelPermalink }}">RSS Feed</a>
{{ end }}
```

请参阅[链接到输出格式][]一节，以理解上述写法的意义。

[associated methods]: /methods/output-format/
[details]: /configuration/output-formats/
[link to output formats]: /configuration/output-formats/#link-to-output-formats

