Custom Output Formats
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:
type | suffix |
---|---|
application/javascript | |
application/json | |
application/octet-stream | |
application/rss+xml | |
application/toml | |
application/xml | |
application/yaml | |
image/svg+xml | |
text/calendar | |
text/css | |
text/csv | |
text/html | |
text/plain | |
text/x-sass | |
text/x-scss |
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
toasp
. - The
Suffix
is the value that will be used for URLs and filenames for that media type in Hugo. - The
Type
is the identifier that must be used when defining new/customOutput 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.
mediaTypes:
text/enriched:
suffix: enr
text/html:
suffix: asp
[mediaTypes]
[mediaTypes."text/enriched"]
suffix = "enr"
[mediaTypes."text/html"]
suffix = "asp"
{
"mediaTypes": {
"text/enriched": {
"suffix": "enr"
},
"text/html": {
"suffix": "asp"
}
}
}
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 output format. So, if you want to change the suffix of the HTML
output format from html
(default) to htm
:
[mediaTypes]
[mediaTypes."text/html"]
suffix = "htm"
# Redefine HTML to update its media type.
[outputFormats]
[outputFormats.HTML]
mediaType = "text/html"
Note that for the above to work, you also need to add an outputs
definition in your site config.
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:
name | mediaType | path | baseName | rel | protocol | isPlainText | isHTML | noUgly |
---|---|---|---|---|---|---|---|---|
AMP | map[suffixes:[html] type:text/html string:text/html mainType:text subType:html delimiter:.] | amp | index | amphtml | false | true | false | |
CSS | map[suffixes:[css] type:text/css string:text/css mainType:text subType:css delimiter:.] | styles | stylesheet | true | false | false | ||
CSV | map[type:text/csv string:text/csv mainType:text subType:csv delimiter:. suffixes:[csv]] | index | alternate | true | false | false | ||
Calendar | map[type:text/calendar string:text/calendar mainType:text subType:calendar delimiter:. suffixes:[ics]] | index | alternate | webcal:// | true | false | false | |
HTML | map[mainType:text subType:html delimiter:. suffixes:[html] type:text/html string:text/html] | index | canonical | false | true | false | ||
JSON | map[suffixes:[json] type:application/json string:application/json mainType:application subType:json delimiter:.] | index | alternate | true | false | false | ||
ROBOTS | map[subType:plain delimiter:. suffixes:[txt] type:text/plain string:text/plain mainType:text] | robots | alternate | true | false | false | ||
RSS | map[suffixes:[xml] type:application/rss+xml string:application/rss+xml mainType:application subType:rss delimiter:.] | index | alternate | false | false | true | ||
Sitemap | map[type:application/xml string:application/xml mainType:application subType:xml delimiter:. suffixes:[xml]] | sitemap | sitemap | false | false | true |
- 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 valueamp
forPath
so it doesn’t overwrite theHTML
version; e.g. we can now have both/index.html
and/amp/index.html
. - The
MediaType
must match theType
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, either for all sites or for a given language.
outputFormats:
MyEnrichedFormat:
baseName: myindex
isPlainText: true
mediaType: text/enriched
protocol: bep://
[outputFormats]
[outputFormats.MyEnrichedFormat]
baseName = "myindex"
isPlainText = true
mediaType = "text/enriched"
protocol = "bep://"
{
"outputFormats": {
"MyEnrichedFormat": {
"baseName": "myindex",
"isPlainText": true,
"mediaType": "text/enriched",
"protocol": "bep://"
}
}
}
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 filename for the list filenames (homepage, etc.). Default:
index
. rel
- can be used to create
rel
values inlink
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. noUgly
- used to turn off ugly URLs If
uglyURLs
is set totrue
in your site. Default:false
. notAlternative
- enable if it doesn’t make sense to include this format in an
AlternativeOutputFormats
format listing onPage
(e.g., withCSS
). Note that we use the term alternative and not alternate here, as it does not necessarily replace the other format. Default:false
.
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
attribute, and the default Output
Formats are set based on that.
Kind | Default Output Formats |
---|---|
page | HTML |
home | HTML, RSS |
section | HTML, RSS |
taxonomyTerm | HTML, RSS |
taxonomy | 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 config file:
outputs:
home:
- HTML
- AMP
- RSS
page:
- HTML
[outputs]
home = ["HTML", "AMP", "RSS"]
page = ["HTML"]
{
"outputs": {
"home": [
"HTML",
"AMP",
"RSS"
],
"page": [
"HTML"
]
}
}
Note that in the above examples, the output formats for section
,
taxonomyTerm
and taxonomy
will stay at their default value ["HTML",
"RSS"]
.
- The
outputs
definition is perPage
Kind
(page
,home
,section
,taxonomy
, ortaxonomyTerm
). - The names (e.g.
HTML
,AMP
) used must match theName
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 YAML
front matter in a content file that defines output formats for the rendered Page
:
---
date: "2016-03-19"
outputs:
- html
- amp
- json
---
Link to 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>
:
{{ range .AlternativeOutputFormats -}}
<link rel="{{ .Rel }}" type="{{ .MediaType.Type }}" href="{{ .Permalink | safeURL }}">
{{ end -}}
Note that .Permalink
and .RelPermalink
on Page
will return the first output format defined for that page (usually HTML
if nothing else is defined).
This is how you link to a given output format:
{{ with .OutputFormats.Get "json" -}}
<a href="{{ .Permalink }}">{{ .Name }}</a>
{{- end }}
From content files, you can use the ref
or relref
shortcodes:
[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.
The following table shows examples of different output formats, the suffix used, and Hugo’s respective template lookup order. All of the examples in the table can:
- Use a base template.
- Include partial templates
Example | OutputFormat | Suffix | Template Lookup Order |
---|---|---|---|
Single page in "posts" section | HTML | html | [layouts/posts/single.html.html layouts/posts/single.html layouts/_default/single.html.html layouts/_default/single.html] |
Single page in "posts" section with layout set | HTML | html | [layouts/posts/demolayout.html.html layouts/posts/single.html.html layouts/posts/demolayout.html layouts/posts/single.html layouts/_default/demolayout.html.html layouts/_default/single.html.html layouts/_default/demolayout.html layouts/_default/single.html] |
AMP single page | AMP | html | [layouts/posts/single.amp.html layouts/posts/single.html layouts/_default/single.amp.html layouts/_default/single.html] |
AMP single page, French language | AMP | html | [layouts/posts/single.fr.amp.html layouts/posts/single.amp.html layouts/posts/single.fr.html layouts/posts/single.html layouts/_default/single.fr.amp.html layouts/_default/single.amp.html layouts/_default/single.fr.html layouts/_default/single.html] |
Home page | HTML | html | [layouts/index.html.html layouts/home.html.html layouts/list.html.html layouts/index.html layouts/home.html layouts/list.html layouts/_default/index.html.html layouts/_default/home.html.html layouts/_default/list.html.html layouts/_default/index.html layouts/_default/home.html layouts/_default/list.html] |
Home page with type set | HTML | html | [layouts/demotype/index.html.html layouts/demotype/home.html.html layouts/demotype/list.html.html layouts/demotype/index.html layouts/demotype/home.html layouts/demotype/list.html layouts/index.html.html layouts/home.html.html layouts/list.html.html layouts/index.html layouts/home.html layouts/list.html layouts/_default/index.html.html layouts/_default/home.html.html layouts/_default/list.html.html layouts/_default/index.html layouts/_default/home.html layouts/_default/list.html] |
Home page with layout set | HTML | html | [layouts/demolayout.html.html layouts/index.html.html layouts/home.html.html layouts/list.html.html layouts/demolayout.html layouts/index.html layouts/home.html layouts/list.html layouts/_default/demolayout.html.html layouts/_default/index.html.html layouts/_default/home.html.html layouts/_default/list.html.html layouts/_default/demolayout.html layouts/_default/index.html layouts/_default/home.html layouts/_default/list.html] |
AMP home, French language" | AMP | html | [layouts/index.fr.amp.html layouts/home.fr.amp.html layouts/list.fr.amp.html layouts/index.amp.html layouts/home.amp.html layouts/list.amp.html layouts/index.fr.html layouts/home.fr.html layouts/list.fr.html layouts/index.html layouts/home.html layouts/list.html layouts/_default/index.fr.amp.html layouts/_default/home.fr.amp.html layouts/_default/list.fr.amp.html layouts/_default/index.amp.html layouts/_default/home.amp.html layouts/_default/list.amp.html layouts/_default/index.fr.html layouts/_default/home.fr.html layouts/_default/list.fr.html layouts/_default/index.html layouts/_default/home.html layouts/_default/list.html] |
JSON home | JSON | json | [layouts/index.json.json layouts/home.json.json layouts/list.json.json layouts/index.json layouts/home.json layouts/list.json layouts/_default/index.json.json layouts/_default/home.json.json layouts/_default/list.json.json layouts/_default/index.json layouts/_default/home.json layouts/_default/list.json] |
RSS home | RSS | xml | [layouts/index.rss.xml layouts/home.rss.xml layouts/rss.xml layouts/list.rss.xml layouts/index.xml layouts/home.xml layouts/list.xml layouts/_default/index.rss.xml layouts/_default/home.rss.xml layouts/_default/rss.xml layouts/_default/list.rss.xml layouts/_default/index.xml layouts/_default/home.xml layouts/_default/list.xml layouts/_internal/_default/rss.xml] |
RSS section posts | RSS | xml | [layouts/posts/section.rss.xml layouts/posts/rss.xml layouts/posts/list.rss.xml layouts/posts/section.xml layouts/posts/list.xml layouts/section/section.rss.xml layouts/section/rss.xml layouts/section/list.rss.xml layouts/section/section.xml layouts/section/list.xml layouts/_default/section.rss.xml layouts/_default/rss.xml layouts/_default/list.rss.xml layouts/_default/section.xml layouts/_default/list.xml layouts/_internal/_default/rss.xml] |
Taxonomy list in categories | RSS | xml | [layouts/categories/category.rss.xml layouts/categories/taxonomy.rss.xml layouts/categories/rss.xml layouts/categories/list.rss.xml layouts/categories/category.xml layouts/categories/taxonomy.xml layouts/categories/list.xml layouts/taxonomy/category.rss.xml layouts/taxonomy/taxonomy.rss.xml layouts/taxonomy/rss.xml layouts/taxonomy/list.rss.xml layouts/taxonomy/category.xml layouts/taxonomy/taxonomy.xml layouts/taxonomy/list.xml layouts/category/category.rss.xml layouts/category/taxonomy.rss.xml layouts/category/rss.xml layouts/category/list.rss.xml layouts/category/category.xml layouts/category/taxonomy.xml layouts/category/list.xml layouts/_default/category.rss.xml layouts/_default/taxonomy.rss.xml layouts/_default/rss.xml layouts/_default/list.rss.xml layouts/_default/category.xml layouts/_default/taxonomy.xml layouts/_default/list.xml layouts/_internal/_default/rss.xml] |
Taxonomy terms in categories | RSS | xml | [layouts/categories/category.terms.rss.xml layouts/categories/terms.rss.xml layouts/categories/rss.xml layouts/categories/list.rss.xml layouts/categories/category.terms.xml layouts/categories/terms.xml layouts/categories/list.xml layouts/taxonomy/category.terms.rss.xml layouts/taxonomy/terms.rss.xml layouts/taxonomy/rss.xml layouts/taxonomy/list.rss.xml layouts/taxonomy/category.terms.xml layouts/taxonomy/terms.xml layouts/taxonomy/list.xml layouts/category/category.terms.rss.xml layouts/category/terms.rss.xml layouts/category/rss.xml layouts/category/list.rss.xml layouts/category/category.terms.xml layouts/category/terms.xml layouts/category/list.xml layouts/_default/category.terms.rss.xml layouts/_default/terms.rss.xml layouts/_default/rss.xml layouts/_default/list.rss.xml layouts/_default/category.terms.xml layouts/_default/terms.xml layouts/_default/list.xml layouts/_internal/_default/rss.xml] |
Section list for "posts" section | HTML | html | [layouts/posts/posts.html.html layouts/posts/section.html.html layouts/posts/list.html.html layouts/posts/posts.html layouts/posts/section.html layouts/posts/list.html layouts/section/posts.html.html layouts/section/section.html.html layouts/section/list.html.html layouts/section/posts.html layouts/section/section.html layouts/section/list.html layouts/_default/posts.html.html layouts/_default/section.html.html layouts/_default/list.html.html layouts/_default/posts.html layouts/_default/section.html layouts/_default/list.html] |
Section list for "posts" section with type set to "blog" | HTML | html | [layouts/blog/posts.html.html layouts/blog/section.html.html layouts/blog/list.html.html layouts/blog/posts.html layouts/blog/section.html layouts/blog/list.html layouts/posts/posts.html.html layouts/posts/section.html.html layouts/posts/list.html.html layouts/posts/posts.html layouts/posts/section.html layouts/posts/list.html layouts/section/posts.html.html layouts/section/section.html.html layouts/section/list.html.html layouts/section/posts.html layouts/section/section.html layouts/section/list.html layouts/_default/posts.html.html layouts/_default/section.html.html layouts/_default/list.html.html layouts/_default/posts.html layouts/_default/section.html layouts/_default/list.html] |
Section list for "posts" section with layout set to "demoLayout" | HTML | html | [layouts/posts/demolayout.html.html layouts/posts/posts.html.html layouts/posts/section.html.html layouts/posts/list.html.html layouts/posts/demolayout.html layouts/posts/posts.html layouts/posts/section.html layouts/posts/list.html layouts/section/demolayout.html.html layouts/section/posts.html.html layouts/section/section.html.html layouts/section/list.html.html layouts/section/demolayout.html layouts/section/posts.html layouts/section/section.html layouts/section/list.html layouts/_default/demolayout.html.html layouts/_default/posts.html.html layouts/_default/section.html.html layouts/_default/list.html.html layouts/_default/demolayout.html layouts/_default/posts.html layouts/_default/section.html layouts/_default/list.html] |
Taxonomy list in categories | HTML | html | [layouts/categories/category.html.html layouts/categories/taxonomy.html.html layouts/categories/list.html.html layouts/categories/category.html layouts/categories/taxonomy.html layouts/categories/list.html layouts/taxonomy/category.html.html layouts/taxonomy/taxonomy.html.html layouts/taxonomy/list.html.html layouts/taxonomy/category.html layouts/taxonomy/taxonomy.html layouts/taxonomy/list.html layouts/category/category.html.html layouts/category/taxonomy.html.html layouts/category/list.html.html layouts/category/category.html layouts/category/taxonomy.html layouts/category/list.html layouts/_default/category.html.html layouts/_default/taxonomy.html.html layouts/_default/list.html.html layouts/_default/category.html layouts/_default/taxonomy.html layouts/_default/list.html] |
Taxonomy term in categories | HTML | html | [layouts/categories/category.terms.html.html layouts/categories/terms.html.html layouts/categories/list.html.html layouts/categories/category.terms.html layouts/categories/terms.html layouts/categories/list.html layouts/taxonomy/category.terms.html.html layouts/taxonomy/terms.html.html layouts/taxonomy/list.html.html layouts/taxonomy/category.terms.html layouts/taxonomy/terms.html layouts/taxonomy/list.html layouts/category/category.terms.html.html layouts/category/terms.html.html layouts/category/list.html.html layouts/category/category.terms.html layouts/category/terms.html layouts/category/list.html layouts/_default/category.terms.html.html layouts/_default/terms.html.html layouts/_default/list.html.html layouts/_default/category.terms.html layouts/_default/terms.html layouts/_default/list.html] |
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:
[partial name].[OutputFormat].[suffix]
The partial below is a plain text template (Outpuf 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
):
{{ partial "mytextpartial.csv" . }}