hugo/content/en/templates/output-formats.md
Bjørn Erik Pedersen aa5ac36a3e Squashed 'docs/' changes from 327003421..39a7fac34
39a7fac34 Add .hugo_build.lock to .gitignore
920c716a4 fix a typo: to -> two (#1545)
6f0ba9593 Remove godocref from front matter (#1543)
8ec3d5948 remove link to wercker (#1544)
b56008719 Delete deployment-with-wercker.md (#1542)
e33d29b02 Fix broken links (#1538)
29e9d4c21 Sort commenting systems (#1541)
0b7ea60a7 Delete the news page "HTTP/2 Server Push in Hugo"
6e1515857 Fix quick-start.md (#1525)
62168ab35 Update comments.md (#1535)
d92191512 Small typo (#1539)
129c8834a Correct the PostCSS noMap default value (#1534)
6a5b29fcc Add example to index function (#1536)
e3dd8c507 Update output-formats.md
0c9321ca0 Remove reference to using LiveReload in production environment
4072d6776 Mod testing
09fabf7d6 Fix typo (#1524)
2fce813c8 Fix grammatical error in quick-start.md (#1523)
45230ab4a Hugo Mod testing
2dd4cd9e7 Update index.md
2c3ed62fd netlify: Bump to 0.88.1
648e2a007 Merge branch 'tempv0.88.1'
f216eade1 releaser: Add release notes to /docs for release of 0.88.1
8a7b64d4b Fix typographical errors in 0.88.0 release notes
a4bf86300 Release 0.88
738bb8f38 releaser: Add release notes to /docs for release of 0.88.0
8fcf2c55d highlight: Remove some pygments references
f2b173de2 HTTPS link
c88881c8e Adding link to nginx documentation
6b0a74fe0 Fix typos in docs (#1516)
498b8f0f1 Fix typos in time.Format (#1515)
28723fad6 Fix taxonomy and term examples (#1514)
3ffd00e12 Update front-matter.md
7cc1da82e Fix grammar in 0.86.1 release notes (#1510)
0009c51c3 Update docs helper
7e2f430f4 Update index.md
7857eae7e releaser: Add release notes to /docs for release of 0.87.0
1f08b684b releaser: Add release notes to /docs for release of 0.87.0
36a9e701c docs: Adjust config docs
0f588438e docs: Regen CLI docs
1b4682cd8 docs: Regen docs helper
bc8bbaae9 Merge commit 'bd77f6e1c99e04a476f0b1bb4e44569134e02399' into release-0.87.0
6f2480643 docs: Adjust time zone docs

git-subtree-dir: docs
git-subtree-split: 39a7fac343c289906db644c96079fdcc0298582f
2021-10-31 13:51:51 +01:00

10 KiB

title linktitle description date publishdate lastmod categories keywords menu weight sections_weight draft aliases toc
Custom Output Formats Custom Output Formats Hugo can output content in multiple formats, including calendar events, e-book formats, Google AMP, and JSON search indexes, or any custom text format. 2017-03-22 2017-03-22 2019-12-11
templates
amp
outputs
rss
docs
parent weight
templates 18
18 18 false
/templates/outputs/
/extras/output-formats/
/content-management/custom-outputs/
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 "media" "types" "type" "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 filenames 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="config" >}} [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:

[mediaTypes]
[mediaTypes."text/html"]
suffixes = ["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:

{{< datatable "output" "formats" "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, either for all sites or for a given language.

{{< code-toggle file="config" >}} [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 filename for the list filenames (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.
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). 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 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 config file:

{{< code-toggle file="config" >}} [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"].

{{< new-in "0.73.0" >}} We have fixed the before confusing page kinds used for taxonomies (see the listing below) to be in line with the terms used when we talk about taxonomies. We have been careful to avoid site breakage, and you should get an ERROR in the console if you need to adjust your outputs section.

{{% page-kinds %}}

  • The outputs definition is per Page Kind (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 YAML front matter in a content file that defines output formats for the rendered Page:

---
date: "2016-03-19"
outputs:
- html
- amp
- json
---

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>:

{{ range .AlternativeOutputFormats -}}
<link rel="{{ .Rel }}" type="{{ .MediaType.Type }}" href="{{ .Permalink | safeURL }}">
{{ end -}}

.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:

{{ .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:

{{ .RelPermalink }} > /that-page/index.json
{{ with  .OutputFormats.Get "html" -}}
{{ .RelPermalink }} > /that-page/
{{- 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.

{{% 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 of the examples in the table can:

{{< 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:

[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):

{{ partial "mytextpartial.csv" . }}