| 1 |
--- |
| 2 |
title: OutputFormats |
| 3 |
description: Returns a slice of OutputFormat objects, each representing one of the output formats enabled for the given page. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: '[]OutputFormat' |
| 9 |
signatures: [PAGE.OutputFormats] |
| 10 |
--- |
| 11 |
|
| 12 |
{{% glossary-term "output format" %}} |
| 13 |
|
| 14 |
The `OutputFormats` method on a `Page` object returns a slice of `OutputFormat` objects, each representing one of the output formats enabled for the given page. See [details][]. |
| 15 |
|
| 16 |
## Methods |
| 17 |
|
| 18 |
Use these methods on the `OutputFormats` object. |
| 19 |
|
| 20 |
`Canonical` |
| 21 |
: {{< new-in 0.154.4 />}} |
| 22 |
: (`page.OutputFormat`) Returns the [canonical output format](g) for the current page, if defined. Once you have captured the object, use any of its [associated methods][]. |
| 23 |
|
| 24 |
```go-html-template |
| 25 |
{{ with .Site.Home.OutputFormats.Canonical }} |
| 26 |
{{ .MediaType.Type }} → text/html |
| 27 |
{{ .MediaType.MainType }} → text |
| 28 |
{{ .MediaType.SubType }} → html |
| 29 |
{{ .Name }} → html |
| 30 |
{{ .Permalink }} → https://example.org/ |
| 31 |
{{ .Rel }} → canonical |
| 32 |
{{ .RelPermalink }} → / |
| 33 |
{{ end }} |
| 34 |
``` |
| 35 |
|
| 36 |
`Get` |
| 37 |
: (`page.OutputFormat`) Returns the `OutputFormat` object with the given identifier. Once you have captured the object, use any of its [associated methods][]. |
| 38 |
|
| 39 |
```go-html-template |
| 40 |
{{ with .Site.Home.OutputFormats.Get "rss" }} |
| 41 |
{{ .MediaType.Type }} → application/rss+xml |
| 42 |
{{ .MediaType.MainType }} → application |
| 43 |
{{ .MediaType.SubType }} → rss |
| 44 |
{{ .Name }} → rss |
| 45 |
{{ .Permalink }} → https://example.org/index.xml |
| 46 |
{{ .Rel }} → alternate |
| 47 |
{{ .RelPermalink }} → /index.xml |
| 48 |
{{ end }} |
| 49 |
``` |
| 50 |
|
| 51 |
## Examples |
| 52 |
|
| 53 |
To render a `link` element pointing to the [canonical output format](g) for the current page: |
| 54 |
|
| 55 |
```go-html-template |
| 56 |
{{ with .OutputFormats.Canonical }} |
| 57 |
{{ printf "<link rel=%q type=%q href=%q>" .Rel .MediaType.Type .Permalink | safeHTML }} |
| 58 |
{{ end }} |
| 59 |
``` |
| 60 |
|
| 61 |
To render an anchor element pointing to the `rss` output format for the current page: |
| 62 |
|
| 63 |
```go-html-template |
| 64 |
{{ with .OutputFormats.Get "rss" }} |
| 65 |
<a href="{{ .RelPermalink }}">RSS Feed</a> |
| 66 |
{{ end }} |
| 67 |
``` |
| 68 |
|
| 69 |
Please see the [link to output formats][] section to understand the importance of the construct above. |
| 70 |
|
| 71 |
[associated methods]: /methods/output-format/ |
| 72 |
[details]: /configuration/output-formats/ |
| 73 |
[link to output formats]: /configuration/output-formats/#link-to-output-formats |