[SOLVED] Current Output Format

Is there a way to get the current output format being used?


So I created a new output format, and I’m using it for pages belonging to a certain section (which, apparently I can’t really do, all pages now have this output format).

For this section, I created a new template specifically for the output format and that’s working well. There’s a small change I want to make in the header.html partial based on the output format. I could simple create a new template for the header, which I’m currently going to do, but it would be much easier to have a variable containing the current output format so that I may use an 'if block instead. I see that for Page variables, .AlternativeOutputFormats and .OutputFormats exists but doesn’t tell me which one is currently being rendered.

1 Like

My backup plan wouldn’t work either since partials aren’t output format aware: Partials Not Working For Output Formats

Is that still the case? The related ticket was closed months ago, and I recall having used an output aware shortcode.

I’m looking for one or both items:

  1. An Output Formats aware partial system - this way I can call {{ partial "header" . }} and if layouts/partials/header.format.html exists, it will use it, otherwise falls back to layouts/partials/header.html.
  2. A Page variable `{{ .CurrentOutputFormat }} which would return the current output format being rendered.

I’m currently going down the route of seeing if I can combine .OutputFormats and .AlternativeOutputFormats. The latter seems to not include the current output format being rendered. So if can use Hugo/Go template functions to compare the two and find the one missing, then I know which format is currently being rendered.

Hey @FelicianoTech, maybe it’s useful for you to:

  1. manually assign the output format of such template in Scratch;
  2. retrieve the output format from Scratch in your partial (as long as you pass the context, you should be able to access it.

For instance, in index.amp.html:

<!doctype html>
<html amp lang="en">
  <head>
    {{ .Scratch.Set "output-format" "amp" }}

then in a partial or the template itself:

      {{ if $.Scratch.Get "output-format" }}
        {{ partial "index/main/entry.amp.html" . }}
      {{ else }}
        {{ partial "index/main/entry.html" . }}
      {{ end }}

Naturally, you could probably do something more sophisticated like formatting the partial path to already include the output-format.

I hope it helps :slight_smile:

thx!

2 Likes

This is actually what I ended up doing as a workaround. Thanks.