Debian Codegraph @

docs/content/en/configuration/output-formats.md

raw
1 ---
2 title: Configure output formats
3 linkTitle: Output formats
4 description: Configure output formats.
5 categories: []
6 keywords: []
7 ---
8
9 {{% glossary-term "output format" %}}
10
11 You can output a page in as many formats as you want. Define an infinite number of output formats, provided they each resolve to a unique file system path.
12
13 This is the default output format configuration in tabular form:
14
15 {{< datatable
16 "config"
17 "outputFormats"
18 "_key"
19 "mediaType"
20 "weight"
21 "baseName"
22 "isHTML"
23 "isPlainText"
24 "noUgly"
25 "notAlternative"
26 "path"
27 "permalinkable"
28 "protocol"
29 "rel"
30 "root"
31 "ugly"
32 >}}
33
34 ## Default configuration
35
36 The following is the default configuration that matches the table above:
37
38 {{< code-toggle config=outputFormats />}}
39
40 `baseName`
41 : (`string`) The base name of the published file. Default is `index`.
42
43 `isHTML`
44 : (`bool`) Whether to classify the output format as HTML. This value determines when the LiveReload script is injected and, in conjunction with [`permalinkable`](#permalinkable), whether [alias redirects][] are generated. Default is `false`.
45
46 `isPlainText`
47 : (`bool`) Whether to parse templates for this output format with Go's [`text/template`][] package instead of the [`html/template`][] package. Default is `false`.
48
49 `mediaType`
50 : (`string`) The [media type](g) of the published file. This must match one of the [configured media types][].
51
52 `notAlternative`
53 : (`bool`) Whether to exclude this output format from the values returned by the [`AlternativeOutputFormats`][] method on a `Page` object. Default is `false`.
54
55 `noUgly`
56 : (`bool`) Whether to disable ugly URLs for this output format when [`uglyURLs`][] are enabled in your project configuration. Default is `false`.
57
58 `path`
59 : (`string`) The first segment of the publication path for this output format. This path segment is relative to the root of your [`publishDir`][]. If omitted, Hugo will use the file's original content path for publishing.
60
61 `permalinkable`
62 : (`bool`) Whether to return the rendering output format rather than the main output format when invoking the [`Permalink`][] and [`RelPermalink`][] methods on a `Page` object. Along with [`isHTML`](#ishtml), this must be `true` to create [alias redirects][]. Enabled by default for the `html` and `amp` output formats. Default is `false`.
63
64 `protocol`
65 : (`string`) The protocol (scheme) of the URL for this output format. For example, `https://` or `webcal://`. Default is the scheme of the [`baseURL`][] parameter in your project configuration, typically `https://`.
66
67 `rel`
68 : (`string`) The relationship of the output format to the current page. Hugo uses this property to determine the [canonical output format](g) of the current page. For the predefined `html` output format, the default value is `canonical`; for all other predefined output formats, the default value is `alternate`.
69
70 `root`
71 : (`bool`) Whether to publish files to the root of the publish directory. Default is `false`.
72
73 `ugly`
74 : (`bool`) Whether to enable uglyURLs for this output format when `uglyURLs` is `false` in your project configuration. Default is `false`.
75
76 `weight`
77 : (`int`) When set to a non-zero value, Hugo uses the `weight` as the first criteria when sorting output formats, falling back to the name of the output format. Lighter items float to the top, while heavier items sink to the bottom. Hugo renders output formats sequentially based on the sort order. Default is `0`, except for the `html` output format, which has a default weight of `10`.
78
79 ## Modify an output format
80
81 You can modify any of the default output formats. For example, to prioritize `json` rendering over `html` rendering, when both are generated, adjust the [`weight`](#weight):
82
83 {{< code-toggle file=hugo >}}
84 [outputFormats.json]
85 weight = 1
86 [outputFormats.html]
87 weight = 2
88 {{< /code-toggle >}}
89
90 The example above shows that when you modify a default content format, you only need to define the properties that differ from their default values.
91
92 ## Create an output format
93
94 You can create new output formats as needed. For example, you may wish to create an output format to support Atom feeds.
95
96 Step 1
97 : Output formats require a specified media type. Because Atom feeds use `application/atom+xml`, which is not one of the [default media types][], you must create it first.
98
99 {{< code-toggle file=hugo >}}
100 [mediaTypes.'application/atom+xml']
101 suffixes = ['atom']
102 {{< /code-toggle >}}
103
104 See [configure media types][] for more information.
105
106 Step 2
107 : Create a new output format:
108
109 {{< code-toggle file=hugo >}}
110 [outputFormats.atom]
111 mediaType = 'application/atom+xml'
112 noUgly = true
113 {{< /code-toggle >}}
114
115 Note that we use the default settings for all other output format properties.
116
117 Step 3
118 : Specify the page [kinds](g) for which to render this output format:
119
120 {{< code-toggle file=hugo >}}
121 [outputs]
122 home = ['html', 'rss', 'atom']
123 section = ['html', 'rss', 'atom']
124 taxonomy = ['html', 'rss', 'atom']
125 term = ['html', 'rss', 'atom']
126 {{< /code-toggle >}}
127
128 See [configure outputs][] for more information.
129
130 Step 4
131 : Create a template to render the output format. Since Atom feeds are lists, you need to create a list template. Consult the [template lookup order][] to find the correct template path:
132
133 ```text
134 layouts/list.atom.atom
135 ```
136
137 We leave writing the template code as an exercise for you. Aim for a result similar to the [embedded RSS template][].
138
139 ## List output formats
140
141 To access output formats, each `Page` object provides two methods: [`OutputFormats`][] (for all formats, including the current one) and [`AlternativeOutputFormats`][]. Use `AlternativeOutputFormats` to create a link `rel` list within a `head` element, as shown below:
142
143 ```go-html-template
144 {{ range .AlternativeOutputFormats }}
145 <link rel="{{ .Rel }}" type="{{ .MediaType.Type }}" href="{{ .Permalink | safeURL }}">
146 {{ end }}
147 ```
148
149 ## Link to output formats
150
151 By default, a `Page` object's [`Permalink`][] and [`RelPermalink`][] methods return the URL of the [primary output format](g), typically `html`. This behavior remains consistent regardless of the template used.
152
153 For example, in `page.json.json`, you'll see:
154
155 ```go-html-template
156 {{ .RelPermalink }} → /that-page/
157 {{ with .OutputFormats.Get "json" }}
158 {{ .RelPermalink }} → /that-page/index.json
159 {{ end }}
160 ```
161
162 To make these methods return the URL of the _current_ template's output format, you must set the [`permalinkable`](#permalinkable) setting to `true` for that format.
163
164 With `permalinkable` set to true for `json` in the same `page.json.json` template:
165
166 ```go-html-template
167 {{ .RelPermalink }} → /that-page/index.json
168 {{ with .OutputFormats.Get "html" }}
169 {{ .RelPermalink }} → /that-page/
170 {{ end }}
171 ```
172
173 ## Template lookup order
174
175 Each output format requires a template conforming to the [template lookup order][].
176
177 For the highest specificity in the template lookup order, include the page kind, output format, and suffix in the file name:
178
179 ```text
180 [page kind].[output format].[suffix]
181 ```
182
183 For example, for section pages:
184
185 Output format|Template path
186 :--|:--
187 `html`|`layouts/section.html.html`
188 `json`|`layouts/section.json.json`
189 `rss`|`layouts/section.rss.xml`
190
191 [`AlternativeOutputFormats`]: /methods/page/alternativeoutputformats/
192 [`OutputFormats`]: /methods/page/outputformats/
193 [`Permalink`]: /methods/page/permalink/
194 [`RelPermalink`]: /methods/page/relpermalink/
195 [`baseURL`]: /configuration/all/#baseurl
196 [`html/template`]: https://pkg.go.dev/html/template
197 [`publishDir`]: /configuration/all/#publishdir
198 [`text/template`]: https://pkg.go.dev/text/template
199 [`uglyURLs`]: /configuration/ugly-urls/
200 [alias redirects]: /content-management/urls/#aliases
201 [configure media types]: /configuration/media-types/
202 [configure outputs]: /configuration/outputs/
203 [configured media types]: /configuration/media-types/
204 [default media types]: /configuration/media-types/
205 [embedded RSS template]: <{{% eturl rss %}}>
206 [template lookup order]: /templates/lookup-order/