1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
---
title: Custom output formats
description: Hugo can output content in multiple formats, including calendar events, e-book formats, Google AMP, and JSON search indexes, or any custom text format.
categories: [fundamentals,templates]
keywords: ["amp", "outputs", "rss"]
menu:
docs:
parent: templates
weight: 210
weight: 210
aliases: [/templates/outputs/,/extras/output-formats/,/content-management/custom-outputs/]
toc: true
---
This page describes how to properly configure your site with the media types and output formats, as well as where to create your templates for your custom outputs.
## Media types
A [media type] (also known as _MIME type_ and _content type_) is a two-part identifier for file formats and format contents transmitted on the internet.
This is the full set of built-in media types in Hugo:
{{< datatable "config" "mediaTypes" "_key" "suffixes" >}}
**Note:**
- It is possible to add custom media types or change the defaults; e.g., if you want to change the suffix for `text/html` to `asp`.
- `Suffixes` are the values that will be used for URLs and file names for that media type in Hugo.
- The `Type` is the identifier that must be used when defining new/custom `Output Formats` (see below).
- The full set of media types will be registered in Hugo's built-in development server to make sure they are recognized by the browser.
To add or modify a media type, define it in a `mediaTypes` section in your [site configuration], either for all sites or for a given language.
{{< code-toggle file="hugo" >}}
[mediaTypes]
[mediaTypes."text/enriched"]
suffixes = ["enr"]
[mediaTypes."text/html"]
suffixes = ["asp"]
{{</ code-toggle >}}
The above example adds one new media type, `text/enriched`, and changes the suffix for the built-in `text/html` media type.
**Note:** these media types are configured for **your output formats**. If you want to redefine one of Hugo's default output formats (e.g. `HTML`), you also need to redefine the media type. So, if you want to change the suffix of the `HTML` output format from `html` (default) to `htm`:
{{< code-toggle file="hugo" >}}
[mediaTypes]
[mediaTypes."text/html"]
suffixes = ["htm"]
[outputFormats]
[outputFormats.HTML]
mediaType = "text/html"
{{</ code-toggle >}}
**Note** that for the above to work, you also need to add an `outputs` definition in your site configuration.
## Output format definitions
Given a media type and some additional configuration, you get an **Output Format**.
This is the full set of Hugo's built-in output formats:
{{< datatable "config" "outputFormats" "name" "mediaType" "path" "baseName" "rel" "protocol" "isPlainText" "isHTML" "noUgly" "permalinkable" >}}
- A page can be output in as many output formats as you want, and you can have an infinite amount of output formats defined **as long as they resolve to a unique path on the file system**. In the above table, the best example of this is `AMP` vs. `HTML`. `AMP` has the value `amp` for `Path` so it doesn't overwrite the `HTML` version; e.g. we can now have both `/index.html` and `/amp/index.html`.
- The `MediaType` must match the `Type` of an already defined media type.
- You can define new output formats or redefine built-in output formats; e.g., if you want to put `AMP` pages in a different path.
To add or modify an output format, define it in an `outputFormats` section in your site's [configuration file](/getting-started/configuration/), either for all sites or for a given language.
{{< code-toggle file="hugo" >}}
[outputFormats.MyEnrichedFormat]
mediaType = "text/enriched"
baseName = "myindex"
isPlainText = true
protocol = "bep://"
{{</ code-toggle >}}
The above example is fictional, but if used for the homepage on a site with `baseURL` `https://example.org`, it will produce a plain text homepage with the URL `bep://example.org/myindex.enr`.
### Configure output formats
The following is the full list of configuration options for output formats and their default values:
`name`
: the output format identifier. This is used to define what output format(s) you want for your pages.
`mediaType`
: this must match the `Type` of a defined media type.
`path`
: sub path to save the output files.
`baseName`
: the base file name for the list file names (homepage, etc.). **Default:** `index`.
`rel`
: can be used to create `rel` values in `link` tags. **Default:** `alternate`.
`protocol`
: will replace the "http://" or "https://" in your `baseURL` for this output format.
`isPlainText`
: use Go's plain text templates parser for the templates. **Default:** `false`.
`isHTML`
: used in situations only relevant for `HTML`-type formats; e.g., page aliases. **Default:** `false`.
`noUgly`
: used to turn off ugly URLs If `uglyURLs` is set to `true` in your site. **Default:** `false`.
`notAlternative`
: enable if it doesn't make sense to include this format in an `AlternativeOutputFormats` format listing on `Page` (e.g., with `CSS`). Note that we use the term _alternative_ and not _alternate_ here, as it does not necessarily replace the other format. **Default:** `false`.
`permalinkable`
: make `.Permalink` and `.RelPermalink` return the rendering Output Format rather than main ([see below](#link-to-output-formats)). This is enabled by default for `HTML` and `AMP`. **Default:** `false`.
`weight`
: Setting this to a non-zero value will be used as the first sort criteria.
## Output formats for pages
A `Page` in Hugo can be rendered to multiple _output formats_ on the file
system.
### Default output formats
Every `Page` has a [`Kind`][page_kinds] attribute, and the default Output
Formats are set based on that.
| Kind | Default Output Formats |
| ---------- | ---------------------- |
| `page` | HTML |
| `home` | HTML, RSS |
| `section` | HTML, RSS |
| `taxonomy` | HTML, RSS |
| `term` | HTML, RSS |
### Customizing output formats
This can be changed by defining an `outputs` list of output formats in either
the `Page` front matter or in the site configuration (either for all sites or
per language).
Example from site configuration file:
{{< code-toggle file="hugo" >}}
[outputs]
home = ["HTML", "AMP", "RSS"]
page = ["HTML"]
{{</ code-toggle >}}
Note that in the above examples, the _output formats_ for `section`,
`taxonomy` and `term` will stay at their default value `["HTML", "RSS"]`.
{{% page-kinds %}}
* The `outputs` definition is per [`Page` `Kind`][page_kinds] (`page`, `home`, `section`, `taxonomy`, or `term`).
* The names (e.g. `HTML`, `AMP`) used must match the `Name` of a defined *Output Format*.
* These names are case insensitive.
* These can be overridden per `Page` in the front matter of content files.
The following is an example of front matter in a content file that defines output formats for the rendered `Page`:
{{< code-toggle file="content/example.md" fm=true copy=false >}}
title: Example
outputs:
- html
- amp
- json
{{< /code-toggle >}}
## List output formats
Each `Page` has both an `.OutputFormats` (all formats, including the current) and an `.AlternativeOutputFormats` variable, the latter of which is useful for creating a `link rel` list in your site's `<head>`:
```go-html-template
{{ range .AlternativeOutputFormats -}}
<link rel="{{ .Rel }}" type="{{ .MediaType.Type }}" href="{{ .Permalink | safeURL }}">
{{ end -}}
```
## Link to output formats
`.Permalink` and `.RelPermalink` on `Page` will return the first output format defined for that page (usually `HTML` if nothing else is defined). This is regardless of the template file they are being called from.
__from `single.json.json`:__
```go-html-template
{{ .RelPermalink }} > /that-page/
{{ with .OutputFormats.Get "json" -}}
{{ .RelPermalink }} > /that-page/index.json
{{- end }}
```
In order for them to return the output format of the current template file instead, the given output format should have its `permalinkable` setting set to true.
**Same template file as above with json output format's `permalinkable` set to true:**
```go-html-template
{{ .RelPermalink }} > /that-page/index.json
{{ with .OutputFormats.Get "html" -}}
{{ .RelPermalink }} > /that-page/
{{- end }}
```
From content files, you can use the [`ref` or `relref` shortcodes](/content-management/shortcodes/#ref-and-relref):
```go-html-template
[Neat]({{</* ref "blog/neat.md" "amp" */>}})
[Who]({{</* relref "about.md#who" "amp" */>}})
```
## Templates for your output formats
A new output format needs a corresponding template in order to render anything useful.
{{% note %}}
The key distinction for Hugo versions 0.20 and newer is that Hugo looks at an output format's `Name` and MediaType's `Suffixes` when choosing the template used to render a given `Page`.
{{% /note %}}
The following table shows examples of different output formats, the suffix used, and Hugo's respective template [lookup order]. All the examples in the table can:
* Use a [base template][base].
* Include [partial templates][partials]
{{< datatable "output" "layouts" "Example" "OutputFormat" "Suffix" "Template Lookup Order" >}}
Hugo will now also detect the media type and output format of partials, if possible, and use that information to decide if the partial should be parsed as a plain text template or not.
Hugo will look for the name given, so you can name it whatever you want. But if you want it treated as plain text, you should use the file suffix and, if needed, the name of the Output Format. The pattern is as follows:
```go-html-template
[partial name].[OutputFormat].[suffix]
```
The partial below is a plain text template (Output Format is `CSV`, and since this is the only output format with the suffix `csv`, we don't need to include the Output Format's `Name`):
```go-html-template
{{ partial "mytextpartial.csv" . }}
```
[base]: /templates/base/
[site configuration]: /getting-started/configuration/
[lookup order]: /templates/lookup-order/
[media type]: https://en.wikipedia.org/wiki/Media_type
[partials]: /templates/partials/
[page_kinds]: /templates/section-templates/#page-kinds
|