Range inside an if statement returns nothing

Hi all,
I have a store I am working on at the moment and using the list.html template with the following code:

list.html

{{ if in (print .RelPermalink) "/store/girls" }}
  {{ range (where .Pages "Type" "store") }}
    {{ partial "store/girls" . }}
  {{ end }}
{{ else if in (print .RelPermalink) "/store/boys" }}
  {{ range (where .Pages "Type" "store") }}
    {{ partial "store/boys" . }}
  {{ end }}
{{ else }}
Store home
{{ end }}

both partials inside the range loop just have a {{ .Title }}

So when I visit /store I see store home, great, but when I visit either /girls or /boys I see a nothing. Now if I remove {{ if in (print .RelPermalink) "/store/girls" }} I see the range and content. Is it possible to have an if and then a range? Still new to Hugo but reading all I can about this.

Also can I have multiple list templates? I tried the lookup order but wasn’t able to get the new templates to override the default list.html template in _default.

Thanks in advance and sorry for my lack of Hugo expertise.

Try the code below.

{{ if hasPrefix .RelPermalink "/store/girls" }}
  {{ range (where .Pages "Type" "store") }}
    {{ partial "store/girls" . }}
  {{ end }}
{{ else if hasPrefix .RelPermalink "/store/boys" }}
  {{ range (where .Pages "Type" "store") }}
   {{ partial "store/boys" . }}
{{ end }}
{{ else }}
  Store home
{{ end }}

Appreciate the quick reply but the range still doesn’t render :frowning:
How could I get the list template to work for girls and boys? girls.list.html? I tried something along those lines but it also didn’t work.

Got it! It has to be {{ range where .Site.Pages "Section" "store" }} thanks to @Mikhail Content Organization: Building a List Template that handles Multi-level subdirectories - #10 by Mikhail

1 Like