Filtering a range based on a translationkey

I want to have a range of pages that is filtered for a certain translationkey. When I use the following, the range is empty:

{{ range where .Site.AllPages ".Params.Translationkey" "eq" "yyy" }}
  xxx
{{ end }}

However, the following works fine:

{{ range .Site.AllPages }}
  {{ if eq .Params.Translationkey "yyy" }}
    xxxxxxx
  {{ end }}
{{ end }}

Am I missing something?

Try:

{{ range where .Site.AllPages ".Translationkey" "eq" "yyy" }}

To answer your original question: We store params in lowercase and the where func does not handle that properly (there is an open issue about it).

Thanks.
It gives an error with your suggested code. The error is “…error calling where: translationkey isn’t a field of struct type *hugolib.Page”

I’ve tried both “translationkey” and “Translationkey”.

OK, that suggests that you run a fairly old Hugo version. You can then try your initial version with all lowercase.

Oh, the following worked:

{{ range where .Site.AllPages ".Params.translationkey" "eq" "pr-dp" }}

I don’t believe I didn’t try all combinations!
I also didn’t think of updating my Hugo since I started using it… I’ll make sure my old code is fine with the new version and then I’ll update.

Thanks!

1 Like