Hugo function to obtain OutputFormat of current page context

Hugo does not currently provide a function to obtain the current OutputFormat โ€” this partial provides a simple solution.

Hugo allows adding custom output formats โ€” yet, up to and including version 0.111.0, it does not offer a function to provide the OutputFormat of the current page context. As Hugo provides two functions to list available OutputFormats and those differ in whether they include the current OutputFormat, a simple call to complement provides the answer.

Note

There is an open issue on GitHub regarding the lack of an OutputFormat property of the Page class. Joe Mooring came up with the same solution as below. But he also adds an important caveat: the below only works as long as all declared OutputFormats have the following declared in config.toml:

1notAlternative = false

You can add the below snippet to layouts/partials/_functions to use it in your templates.

 1{{- /* partial output-format
 2Returns the [OutputFormat](https://gohugo.io/templates/output-formats/)
 3of the page passed as argument in the current context.
 4USAGE: use as a function in the context of a page, as follows:
 5  {{- outputFormat := partial "partials/_functions/output-format" . }}
 6*/ -}}
 7{{- $outputFormat := false }}
 8{{- with (complement $.AlternativeOutputFormats $.OutputFormats) }}
 9  {{- $outputFormat = index . 0 }}
10{{- end }}
11{{- return $outputFormat }}
output-format

Photo by Keith Misner on Unsplash.