Conditionals in a range

I am trying to make a card-based layout where different types (using type-filter) has a slightly different design, however, I want them to go in the same grid and sorted by date, irregardless of type, so the latest are always first.

I am struggling with how to code this up with hugo. I tried using range, with nested if’s but that does not work. I have also looked at where and nested where, but I cannot find the proper way to do it.

1 Like

Have you tried looking at intersect and union to build your “mutli-type” collection and then sort it by date?

I looked at those as well, but I am not sure it acheives what I want.

I’ll try to illustrate.

  {{ range where .Site.Pages "Section" "blog"}}
    {{ if "type" eq "podcast" }}
      Do something
    {{ elseif "type" eq "link"}}
    Do something
    {{end}}
    {{else}}
    Do the general thing
  {{ end }}

Write it this way. In hugo templating, eq is a function that takes 2 arguments.

{{ range where site.RegularPages "Section" "blog" }}
  {{ if eq .Type "podcast" }}
    ...
  {{ else if eq .Type "link" }}
    ...
  {{ else }}
    ...
  {{ end }}
{{ end }}
1 Like