hugo/content/templates/output-formats.md
Bjørn Erik Pedersen 2c0d1ccdcd Squashed 'docs/' changes from b0470688..73f355ce
73f355ce Update theme
83ff50c2 Use example.com in examples
71292134 Add alias news > release-notes
2e15f642 Update theme
8eef09d2 Add Pygments configuration
572b9e75 Clean up the code shortcode use
a1b2fd3b Remove the code fence language codes
1473b1d9 Remove redundant text
b92c2042 Update theme
8f439c28 Edit contributing section in README
8bcf8a19 Add contributing section to README
4c44ee1c Fix broken content file
2bdc7710 Clarify .Data.Pages sorting in lists.md
092271c2 Use infinitive mood for main titles
b9b8abef Update theme to reflect change to home page content
b897b71b Change copy to use sentence case
fd675ee5 Enable RSS feed for sections
060a5e27 Correct movie title in taxonomies.md
6a5ca96a Update displayed site name for Hub
22f4b7a4 Add example of starting up the local server
d9612cb3 Update theme
a8c3988a Update theme
4198189d Update theme
12d6b016 Update theme
2b1c4197 Update theme
b6d90a1e Fix News release titles
cfe751db Add some build info to README

git-subtree-dir: docs
git-subtree-split: 73f355ce0dd88d032062ea70067431ab980cdd8d
2017-07-21 11:00:08 +02:00

7.9 KiB

title linktitle description date publishdate lastmod categories 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 2017-03-22
templates
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" "Suffix" >}}

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.
  • 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/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.

Example in config.toml:

[mediaTypes]
  [mediaTypes."text/enriched"]
  suffix = "enr"
  [mediaTypes."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.

Output Formats

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">}}

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

[outputFormats.MyEnrichedFormat]
mediaType = "text/enriched"
baseName = "myindex"
isPlainText = true
protocol = "bep://"

The above example is fictional, but if used for the homepage on a site with baseURL http://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.

Output Formats for Pages

A Page in Hugo can be rendered to multiple representations on the file system. By default, all pages will render as HTML with some of them also as RSS (homepage, sections, etc.).

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

[outputs]
  home = ["HTML", "AMP", "RSS"]
  page = ["HTML"]

Example from site config.yml:

outputs:
  home: ["HTML", "AMP", "RSS"]
  page: ["HTML"]
  • The output definition is per Page Kind (i.e, page, home, section, taxonomy, or taxonomyTerm).
  • The names used must match the Name of a defined Output Format.
  • Any Kind without a definition will default to HTML.
  • These can be overridden per Page in the front matter of content files.
  • Output formats are case insensitive.

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

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.

{{% note %}} The key distinction for Hugo versions 0.20 and newer is that Hugo looks at an output format's Name and MediaType's Suffix 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 (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" . }}