Permalink node pages - if in issue


I have the following code in my default_head.html:

Page/Node permalink: {{ .Permalink }}
{{ if in .Permalink "categoryname" }}
    This is a match
{{ end }}

On my section page it prints:

Page/Node permalink: http://localhost:1313/categoryname/

On posts belonging to that section it prints:

Page/Node permalink: http://localhost:1313/categoryname/post-name/ This is a match

Why isn’t the match triggered on the section pages? After all, the print output shows that the .Permalink contains categoryname, and so I think that if in .Permalink "categoryname" should return true and print the “this is a match” message.

(Hugo v0.14)

This is a curious bug, but the solution will only “make it worse” for you.

The thing is:

  • Permalink on Page is of type string
  • Permalink on Node (section etc.) is of type template.HTML

Both should be template.URL I guess … maybe, but that would break stuff … but in any case the in func matches on both type and content.

A workaround that should work in both cases is:

{{ if in ( printf "%v" .Permalink) "categoryname" }}

The above should make sure that you get a string before doing the compare.

Could you create a GitHub issue for this?

Thank you, that works. Another workaround I found is using .URL for the node page and .Permalink for the post page:

{{ if or (in .Permalink "/categoryname/") (eq .URL "/categoryname/") }}

This .URL variable didn’t work (for me) on post pages in an if in statement (since .URL is empty on post pages).

I’ve made the Github issue here: