Strange behaviour .MediaType and eq

This gives TRUE:

{{- if in (resources.GetMatch .Destination).MediaType "image/jpeg" -}}

while this gives FALSE:

{{- if eq (resources.GetMatch .Destination).MediaType "image/jpeg" -}}


According to the docs, in checks if an element is in an array or slice–or a substring in a string, so the media type may contain suffix or parameters.

Yup… but here is the fun part: The following returns image/jpeg:

(resources.GetMatch .Destination).MediaType

There is no MediaType that contains image/jpeg and is not image/jpeg. Or is there?

Feel free to try this on your own machine. It is easy to reproduce.

I forgot that the MediaType is a struct, not a string, eq returns false when the two arguments aren’t in the same type.

{{- $type :=(resources.GetMatch .Destination).MediaType }}
{{- if eq $type.Type "image/png" -}}

Great! I see that this gives TRUE:

{{- if eq (string (resources.GetMatch .Destination).MediaType) "image/jpeg" -}}

Thank you for solving this mystery!


{{ $type :=(resources.GetMatch .Destination).MediaType }}
{{ $type.Type => “image/jpeg” }}
{{ $type | string => “image/jpeg” }}

We’re currently working on a revamped Hugo documentation with a complete reference section, including all functions, methods and types in use in Hugo (including MediaType). I see we need to speed up that work …


Thanks, didn’t know there is a Type function, just printf "%#v" of the media type.

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