github.com/lyeb/hugo@v0.47.1/docs/content/en/templates/output-formats.md (about)

     1  ---
     2  title: Custom Output Formats
     3  linktitle: Custom Output Formats
     4  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.
     5  date: 2017-03-22
     6  publishdate: 2017-03-22
     7  lastmod: 2017-03-22
     8  categories: [templates]
     9  keywords: ["amp","outputs","rss"]
    10  menu:
    11    docs:
    12      parent: "templates"
    13      weight: 18
    14  weight: 18
    15  sections_weight: 18
    16  draft: false
    17  aliases: [/templates/outputs/,/extras/output-formats/,/content-management/custom-outputs/]
    18  toc: true
    19  ---
    20  
    21  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.
    22  
    23  ## Media Types
    24  
    25  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.
    26  
    27  This is the full set of built-in media types in Hugo:
    28  
    29  {{< datatable "media" "types" "type" "suffix" >}}
    30  
    31  **Note:**
    32  
    33  * 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`.
    34  * The `Suffix` is the value that will be used for URLs and filenames for that media type in Hugo.
    35  * The `Type` is the identifier that must be used when defining new/custom `Output Formats` (see below).
    36  * 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.
    37  
    38  To add or modify a media type, define it in a `mediaTypes` section in your [site configuration][config], either for all sites or for a given language.
    39  
    40  {{< code-toggle file="config" >}}
    41  [mediaTypes]
    42    [mediaTypes."text/enriched"]
    43    suffix = "enr"
    44    [mediaTypes."text/html"]
    45    suffix = "asp"
    46  {{</ code-toggle >}}
    47  
    48  The above example adds one new media type, `text/enriched`, and changes the suffix for the built-in `text/html` media type.
    49  
    50  **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`:
    51  
    52  ```toml
    53  [mediaTypes]
    54  [mediaTypes."text/html"]
    55  suffix = "htm"
    56  
    57  # Redefine HTML to update its media type.
    58  [outputFormats]
    59  [outputFormats.HTML]
    60  mediaType = "text/html"
    61  ```
    62  
    63  **Note** that for the above to work, you also need to add an `outputs` definition in your site config.
    64  
    65  ## Output Format Definitions
    66  
    67  Given a media type and some additional configuration, you get an **Output Format**.
    68  
    69  This is the full set of Hugo's built-in output formats:
    70  
    71  {{< datatable "output" "formats" "name" "mediaType" "path" "baseName" "rel" "protocol" "isPlainText" "isHTML" "noUgly">}}
    72  
    73  * 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`.
    74  * The `MediaType` must match the `Type` of an already defined media type.
    75  * 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.
    76  
    77  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.
    78  
    79  {{< code-toggle file="config" >}}
    80  [outputFormats.MyEnrichedFormat]
    81  mediaType = "text/enriched"
    82  baseName = "myindex"
    83  isPlainText = true
    84  protocol = "bep://"
    85  {{</ code-toggle >}}
    86  
    87  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`.
    88  
    89  ### Configure Output Formats
    90  
    91  The following is the full list of configuration options for output formats and their default values:
    92  
    93  `name`
    94  : the output format identifier. This is used to define what output format(s) you want for your pages.
    95  
    96  `mediaType`
    97  : this must match the `Type` of a defined media type.
    98  
    99  `path`
   100  : sub path to save the output files.
   101  
   102  `baseName`
   103  : the base filename for the list filenames (homepage, etc.). **Default:** `index`.
   104  
   105  `rel`
   106  : can be used to create `rel` values in `link` tags. **Default:** `alternate`.
   107  
   108  `protocol`
   109  : will replace the "http://" or "https://" in your `baseURL` for this output format.
   110  
   111  `isPlainText`
   112  : use Go's plain text templates parser for the templates. **Default:** `false`.
   113  
   114  `isHTML`
   115  : used in situations only relevant for `HTML`-type formats; e.g., page aliases.
   116  
   117  `noUgly`
   118  : used to turn off ugly URLs If `uglyURLs` is set to `true` in your site. **Default:** `false`.
   119  
   120  `notAlternative`
   121  : 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`.
   122  
   123  ## Output Formats for Pages
   124  
   125  A `Page` in Hugo can be rendered to multiple *output formats* on the file
   126  system.
   127  
   128  ### Default Output Formats
   129  Every `Page` has a [`Kind`][page_kinds] attribute, and the default Output
   130  Formats are set based on that.
   131  
   132  | Kind           | Default Output Formats |
   133  |--------------- |----------------------- |
   134  | `page`         | HTML                   |
   135  | `home`         | HTML, RSS              |
   136  | `section`      | HTML, RSS              |
   137  | `taxonomyTerm` | HTML, RSS              |
   138  | `taxonomy`     | HTML, RSS              |
   139  
   140  ### Customizing Output Formats
   141  
   142  This can be changed by defining an `outputs` list of output formats in either
   143  the `Page` front matter or in the site configuration (either for all sites or
   144  per language).
   145  
   146  Example from site config file`:
   147  
   148  {{< code-toggle file="config" >}}
   149  [outputs]
   150    home = ["HTML", "AMP", "RSS"]
   151    page = ["HTML"]
   152  {{</ code-toggle >}}
   153  
   154  
   155  Note that in the above examples, the *output formats* for `section`,
   156  `taxonomyTerm` and `taxonomy` will stay at their default value `["HTML",
   157  "RSS"]`.
   158  
   159  * The `outputs` definition is per [`Page` `Kind`][page_kinds] (`page`, `home`, `section`, `taxonomy`, or `taxonomyTerm`).
   160  * The names (e.g. `HTML`, `AMP`) used must match the `Name` of a defined *Output Format*.
   161    * These names are case insensitive.
   162  * These can be overridden per `Page` in the front matter of content files.
   163  
   164  The following is an example of `YAML` front matter in a content file that defines output formats for the rendered `Page`:
   165  
   166  ```yaml
   167  ---
   168  date: "2016-03-19"
   169  outputs:
   170  - html
   171  - amp
   172  - json
   173  ---
   174  ```
   175  
   176  ## Link to Output Formats
   177  
   178  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>`:
   179  
   180  ```go-html-template
   181  {{ range .AlternativeOutputFormats -}}
   182  <link rel="{{ .Rel }}" type="{{ .MediaType.Type }}" href="{{ .Permalink | safeURL }}">
   183  {{ end -}}
   184  ```
   185  
   186  Note that `.Permalink` and `.RelPermalink` on `Page` will return the first output format defined for that page (usually `HTML` if nothing else is defined).
   187  
   188  This is how you link to a given output format:
   189  
   190  ```go-html-template
   191  {{ with  .OutputFormats.Get "json" -}}
   192  <a href="{{ .Permalink }}">{{ .Name }}</a>
   193  {{- end }}
   194  ```
   195  
   196  From content files, you can use the [`ref` or `relref` shortcodes](/content-management/shortcodes/#ref-and-relref):
   197  
   198  ```go-html-template
   199  [Neat]({{</* ref "blog/neat.md" "amp" */>}})
   200  [Who]({{</* relref "about.md#who" "amp" */>}})
   201  ```
   202  
   203  ## Templates for Your Output Formats
   204  
   205  A new output format needs a corresponding template in order to render anything useful.
   206  
   207  {{% note %}}
   208  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`.
   209  {{% /note %}}
   210  
   211  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:
   212  
   213  * Use a [base template][base].
   214  * Include [partial templates][partials]
   215  
   216  {{< datatable "output" "layouts" "Example" "OutputFormat" "Suffix" "Template Lookup Order" >}}
   217  
   218  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.
   219  
   220  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:
   221  
   222  ```
   223  [partial name].[OutputFormat].[suffix]
   224  ```
   225  
   226  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`):
   227  
   228  ```
   229  {{ partial "mytextpartial.csv" . }}
   230  ```
   231  
   232  [base]: /templates/base/
   233  [config]: /getting-started/configuration/
   234  [lookup order]: /templates/lookup/
   235  [media type]: https://en.wikipedia.org/wiki/Media_type
   236  [partials]: /templates/partials/
   237  [page_kinds]: /templates/section-templates/#page-kinds