Add shuffle to range on condition

A bit of a weird question.

I’m listing recent posts from different sections. For one of these sections, I’d like to apply shuffle to the range. Is there a better way of doing this than I’ve done below as it’s repetitive?

{{$pages := where site.RegularPages "Section" (lower .heading)}}    
{{if eq .heading "Products"}}
   {{range $pages | shuffle | first 5}}
     {{.Title}}
   {{end}}
{{else}}
   {{range $pages | first 5}}
    {{.Title}}
  {{end}}
{{end}}

A bit shorter:

{{$pages := where site.RegularPages "Section" (lower .heading)}}    
{{if eq .heading "Products"}}
 {{$pages = $pages | shuffle}}
{{end}}
{{range $pages | first 5}}
  {{.Title}}
{{end}}
2 Likes

Nice one! Thanks.