Sorting items into two lists on same page...possible?

Hi folks,
I essentially have two html lists. What I want to do is iterate over a range of some content and put it either in list 1 or list 2. Whether it goes onto list 1 or list 2 depends on some variable in the content AND whether the previous content went onto list 1 or list 2. I dont see how this is possible because every content the template engine sees in every iteration of the range function will cause it to duplicate the html code in between. I don’t see how this is possible without a goto of some sort.

could i build the lists internally in memory somehow, and then create one at a time in the template? i.e, i run the range function, and put each item either in array 1 or array 2, and then create a list with all the items in array 1 and another list with all the items in array 2?

You don’t have to output html when you range:

{{ $list1 := slice }}
{{ $list2 := slice }}

{{ range $items }}
  {{ if $condition1 }}
    {{ $list1 = $list1 | append . }}
  {{ else }}
    {{ $list2 = $list2 | append . }}
  {{ end }}
{{ end }}

{{ range $list1 }}
{{ end }}

{{ range $list2 }}
{{ end }}


3 Likes

sweet cheeses! Thank you! I wasnt sure if i could append an entire “.” to a list. This solves so many problems!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.