How to recognize which output format being rendering?

[outputs]
home = ["HTML", "Search"]

Hi all, I want to include some JS and CSS only on the Search output (use same baseof.html), but I couldn’t recognize whether I’m on the search page, since the .RelPermalink always return the default format (HTML) URL.

I’m wondering if it’s safe to use Store in this case?

// baseof.html
{{ if .Store.Get "search" }}
<link rel="stylesheet" herf="css/search.css">
{{ end  }}

{{ block "main" . }}{{ end }}
// index.search.html
{{ define "main" }}
  {{ .Store.Set "search" true }}
  SEARCH PAGE
{{ end }}

EDITED:

The Store doesn’t work, there is a workaround that create a duplicate baseof template named baseof.search.html, but it’s not a good choice :frowning:

EDITED #2

There is other workaround by using block, but it is still a bit complicated.

// baseof.html
<html>
<head>
 {{ block "head-end" . }}{{ end }}
</head>
<body>
{{ block "main" . }}{{ end }}
</body>
<html>
// index.search.html
{{ define "head-end" }}
<link rel="stylesheet" herf="css/search.css">
{{ end }}

{{ define "main" }}
SEARCH PAGE
{{ end }}
1 Like

https://github.com/gohugoio/hugo/issues/9368

This works:

{{ (index (complement .AlternativeOutputFormats .OutputFormats) 0).Name }}
1 Like

Thanks a lot, but it’s not 100 percent works (reliable) in the case of multiple output formats (all of them are not alternative), since I have a PWA module, the final outputs of my site may look like as following.

[outputs]
home = ["HTML", "Offline", "Search", "SearchIndex", "WebManifest"]

It relies on the order of the outputs.

To clarify, your site configuration has something like:

[outputFormats.FOO]
notAlternative = true

Yes?

Yes, I set notAlternative as true for those formats.