Output format not working for partials

Hi guys,

I’m trying to implement a custom partial for regular view and another for AMP.

My partial usage is:

{{ partial "ads.html" (dict "slot" "XXXXX" "responsive" "true" "format" "auto" "context" .) }}

I created two files partials/ads.amp.html and partials/ads.html. But, when I include it on an AMP page, it renders only the regular version.

In the documentation, the usage are [partial name].[OutputFormat].[suffix]

It is similar to Partials Not Working For Output Formats - #3 by felipetoffolo1, but now the feature was merged.

How I can switch between both versions with templating names?

PS: I also tried ads.html.html and ads.amp.html.

I found two references in the documentation.

  • https://gohugo.io/templates/partials/#partial-template-lookup-order

    This is pretty clear (emphasis mine):

    Partial templates—like single page templates and list page templates—have a specific lookup order. However, partials are simpler in that Hugo will only check in two places:

    layouts/partials/*<PARTIALNAME>.html
    themes/<THEME>/layouts/partials/*<PARTIALNAME>.html

    I have no idea why there’s an asterisk to the left of the PARTIAL name.

  • https://gohugo.io/templates/output-formats#readout

    This is confusing:

    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]

    Although I am confused by these paragraphs, they are not related to lookup order.

The first reference above is correct and verified through testing. If you want to call partials/ads.amp.html, then you have to:

{{ partial "ads.amp.html" . }}

Try it:

git clone --single-branch -b hugo-forum-topic-35950 https://github.com/jmooring/hugo-testing hugo-forum-topic-35950
cd hugo-forum-topic-35950
hugo server

Hi @jmooring, thanks for the response.

Indeed, It does not work for partials. In the linked issue, seem to work only for shortcodes (Support shortcode per output format · Issue #3220 · gohugoio/hugo · GitHub).

As workaround, I created a conditional inside the partial template:

{{ if .OutputFormats.Get "AMP" }}
<!--  AMP AD -->
{{ else }}
<!--  Normal AD -->
{{ end }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.